Problem C
Character Recognition?
Write a program that recognizes characters. Don't worry, because you only need to recognize three digits: 1, 2 and 3. Here they are:
.*. *** *** .*. ..* ..* .*. *** *** .*. *.. ..* .*. *** ***
Input
The input contains only one test case, consisting of 6 lines. The first line contains n, the number of characters to recognize (1<=n<=10). Each of the next 5 lines contains 4n characters. Each character contains exactly 5 rows and 3 columns of characters followed by an empty column (filled with '.').
Output
The output should contain exactly one line, the recognized digits in one line.
Sample Input
3 .*..***.***. .*....*...*. .*..***.***. .*..*.....*. .*..***.***.
Output for the Sample Input
123
The Ninth Hunan Collegiate Programming Contest (2013) Problemsetter: Rujia Liu Special Thanks: Feng Chen, Md. Mahbubul Hasan
方法很多,深入理解dfs即可方便解决此题 ,我觉得这个方法比较好,有一定的价值。
#include <iostream> #include <stdio.h> #include <queue> #include <stdio.h> #include <string.h> #include <vector> #include <queue> #include <set> #include <algorithm> #include <map> #include <stack> #include <math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std ; typedef long long LL ; struct Point{ int X ; int Y ; Point(){} ; Point(int x ,int y):X(x),Y(y){} ; }; char str[48][48] ; int N ; int d[4][2]={{1,0},{-1,0},{0,-1},{0,1}} ; bool visited[48][48] ; vector<Point>my_hash[18] ; int cango(int x ,int y){ return 1<=x&&x<=5&&1<=y&&y<=4*N&&str[x][y]=='*'; } void dfs(int x ,int y ,int color){ my_hash[color].push_back(Point(x,y)) ; visited[x][y]=1 ; for(int i=0;i<4;i++){ int xx=x+d[i][0] ; int yy=y+d[i][1] ; if(cango(xx,yy)&&!visited[xx][yy]){ dfs(xx,yy,color) ; } } } int main(){ int color=0 ; scanf("%d",&N) ; for(int i=1;i<=5;i++) scanf("%s",str[i]+1) ; for(int i=1;i<=N;i++) my_hash[i].clear() ; for(int i=1;i<=5;i++) for(int j=1;j<=4*N;j++){ if(str[i][j]=='*'&&!visited[i][j]){ color++ ; dfs(i,j,color) ; } } for(int i=1;i<=N;i++){ Point first = my_hash[i][0] ; Point second = my_hash[i][my_hash[i].size()-1] ; if(first.Y==second.Y&&first.X+4==second.X) putchar('1') ; else if(first.Y<second.Y) putchar('2') ; else if(first.X+2==second.X) putchar('3') ; } puts("") ; return 0 ; }