Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.
A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.
Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').
Consider sample tests in order to understand the snake pattern.
The only line contains two integers: n and m (3 ≤ n, m ≤ 50).
n is an odd number.
Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.
3 3
### ..# ###
3 4
#### ...# ####
5 3
### ..# ### #.. ###
9 9
######### ........# ######### #........ ######### ........# ######### #........ #########
第一题,直接模拟一下输出。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> using namespace std ; int Map[60][60] ; int main() { int i , j , n, m , flag ; while( scanf("%d %d", &n, &m) != EOF ) { memset(Map,0,sizeof(Map)) ; flag = 0 ; for(i = 0 ; i < n ; i++) { for(j = 0 ; j < m ; j++) Map[i][j] = 1 ; if( flag == 0 ) { Map[++i][m-1] = 1 ; flag = 1 ; } else { Map[++i][0] = 1 ; flag = 0 ; } } for(i = 0 ; i < n ; i++) { for(j = 0 ; j < m ; j++) { if( Map[i][j] == 1 ) printf("#") ; else printf(".") ; } printf("\n") ; } } return 0 ; }
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:
Determine if there exists a cycle on the field.
The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output "Yes" if there exists a cycle, and "No" otherwise.
3 4 AAAA ABCA AAAA
Yes
3 4 AAAA ABCA AADA
No
4 4 YYYR BYBY BBBY BBBY
Yes
7 6 AAAAAB ABBBAB ABAAAB ABABBB ABAAAB ABBBAB AAAAAB
Yes
2 13 ABCDEFGHIJKLM NOPQRSTUVWXYZ
No
In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).
第二个找出是否存在环,dfs,对每一个遍历的数进行标号,当遍历的点的周围存在已经遍历过的点是,判断,如果当前点的标号 > 已遍历点的标号+1,(也就是说这两个点存在一条路径相连)那么存在环。
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std ; struct node { int x,y ; }; queue <node> que ; char str[60][60] ; int Map[60][60] ; int a[4][2] = { {0,-1},{0,1},{1,0},{-1,0} } ; int dfs(int u,int v,int n,int m) { int i , x, y ; //printf("%d %d %d\n", u, v, Map[u][v]) ; for(i = 0 ; i < 4 ; i++) { x = u + a[i][0] ; y = v + a[i][1] ; if( x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != -1 && Map[u][v] > Map[x][y]+1 ) return 1 ; } for(i = 0 ; i < 4 ; i++) { x = u + a[i][0] ; y = v + a[i][1] ; if( x >= 0 && x < n && y >= 0 && y < m && str[x][y] == str[u][v] && Map[x][y] == -1 ) { Map[x][y] = Map[u][v]+1 ; if( dfs(x,y,n,m) ) return 1 ; } } str[u][v] = '*' ; return 0; } int main() { int n , m , i , j ; scanf("%d %d", &n, &m) ; for(i = 0 ; i < n ; i++) scanf("%s", str[i]) ; memset(Map,-1,sizeof(Map)) ; for(i = 0 ; i < n ; i++) { for(j = 0 ; j < m ; j++) { memset(Map,-1,sizeof(Map)) ; if( str[i][j] != '*' ) { Map[i][j] = 1; if( dfs(i,j,n,m) ) break ; } } if( j < m ) break ; } if( i < n ) printf("Yes\n") ; else printf("No\n") ; return 0 ; }