UVA - 657 - The die is cast (DFS 2 회!!)
The die is cast
Time Limit: 3000MS
Memory Limit: Unknown
64bit IO Format: %lld & %llu
Submit Status
Description
The die is cast
InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential customers. Be it Monopoly, ludo or backgammon, most of these games involve throwing dice at some stage of the game.
Of course, it would be unreasonable if players were allowed to throw their dice and then enter the result into the computer, since cheating would be way to easy. So, instead, InterGames has decided to supply their users with a camera that takes a picture of the thrown dice, analyzes the picture and then transmits the outcome of the throw automatically.
For this they desperately need a program that, given an image containing several dice, determines the numbers of dots on the dice.
We make the following assumptions about the input images. The images contain only three dif- ferent pixel values: for the background, the dice and the dots on the dice. We consider two pixels connected if they share an edge - meeting at a corner is not enough. In the figure, pixels A and B are connected, but B and C are not.
A set S of pixels is connected if for every pair (a,b) of pixels in S, there is a sequence in S such that a = a1 and b = ak , and ai and ai+1 are connected for .
We consider all maximally connected sets consisting solely of non-background pixels to be dice. `Maximally connected' means that you cannot add any other non-background pixels to the set without making it dis-connected. Likewise we consider every maximal connected set of dot pixels to form a dot.
Input The input consists of pictures of several dice throws. Each picture description starts with a line containing two numbers w and h, the width and height of the picture, respectively. These values satisfy .
The following h lines contain w characters each. The characters can be: ``.'' for a background pixel, ``*'' for a pixel of a die, and ``X'' for a pixel of a die's dot.
Dice may have different sizes and not be entirely square due to optical distortion. The picture will contain at least one die, and the numbers of dots per die is between 1 and 6, inclusive.
The input is terminated by a picture starting with w = h = 0, which should not be processed.
Output For each throw of dice, first output its number. Then output the number of dots on the dice in the picture, sorted in increasing order.
Print a blank line after each test case.
Sample Input
30 15
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
0 0
Sample Output
Throw 1
1 2 2 4
Miguel Revilla 2000-05-22
Source
Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 4. Graph :: Depth First Search :: Finding Connected Components / Flood Fill
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Graphs
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Graph Traversal :: Flood Fill/Finding Connected Components
Root :: Western and Southwestern European Regionals :: 1998 - Ulm
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Graph Traversal :: Flood Fill/Finding Connected Components
제목: 아마 모든 dice 에 연 결 된 dots 의 갯 수 (두 개의 dots 가 연 결 된 판정 조건 은 그들의 연결) 를 찾 는 것 일 것 입 니 다. 출력 할 때 작은 것 에서 큰 것 으로 출력 합 니 다. 그리고 각 그룹의 데 이 터 를 주의 한 후에 빈 줄 이 있어 야 합 니 다.
사고방식: 두 번 DFS, 먼저 dice 를 찾 은 다음 에 dice 에 연 결 된 dots 의 개 수 를 찾 아 라
AC 코드:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int dir[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
const int maxn = 55;
char map[maxn][maxn];
int vis[maxn][maxn];
int ans[1300];
int num, w, h;
void dfs2(int x, int y)
{
if(map[x][y] == '*' || map[x][y] == '.' || vis[x][y]) return;
vis[x][y] = 1;
for(int i=0; i<4; i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=h||yy>=w||xx<0||yy<0||map[xx][yy]=='*') // ,RE
continue;
dfs2(xx, yy);
}
}
void dfs(int x, int y)
{
if(map[x][y] == '.' || vis[x][y]) return;
if(!vis[x][y] && map[x][y] == 'X') // dice dots DFS
{
ans[num]++;
dfs2(x, y);
}
vis[x][y] = 1;
for(int i=0; i<4; i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=h||yy>=w||xx<0||yy<0||map[xx][yy]=='.') // ,RE
continue;
dfs(xx,yy);
}
}
int main()
{
int cas = 1;
while(scanf("%d %d", &w, &h), w || h)
{
char a[55];
for(int i=0; i<h; i++)
{
scanf("%s", a);
for(int j=0; j<w; j++)
{
map[i][j] = a[j];
}
}
memset(vis, 0, sizeof(vis));
memset(ans, 0, sizeof(ans));
num = 0;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(!vis[i][j] && map[i][j] == '*') // dice DFS
{ dfs(i, j); num++; }
}
}
sort(ans, ans+num);
printf("Throw %d
", cas++);
for(int i=0; i<num-1; i++)
{
if(ans[i]) printf("%d ", ans[i]);
}
printf("%d
", ans[num-1]);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.