codeforces510--A. Fox And Snake--B. Fox And Two Dots

A. Fox And Snake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The only line contains two integers: n and m (3 ≤ n, m ≤ 50).

n is an odd number.

Output

Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.

Sample test(s)
input
3 3
output
###
..#
###
input
3 4
output
####
...#
####
input
5 3
output
###
..#
###
#..
###
input
9 9
output
#########
........#
#########
#........
#########
........#
#########
#........
#########

 

 

第一题,直接模拟一下输出。

#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 ;
}


 

 

 

 

 

B. Fox And Two Dots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

codeforces510--A. Fox And Snake--B. Fox And Two Dots_第1张图片

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:

  1. These k dots are different: if i ≠ j then di is different from dj.
  2. k is at least 4.
  3. All dots belong to the same color.
  4. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.

Determine if there exists a cycle on the field.

Input

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

Output "Yes" if there exists a cycle, and "No" otherwise.

Sample test(s)
input
3 4
AAAA
ABCA
AAAA
output
Yes
input
3 4
AAAA
ABCA
AADA
output
No
input
4 4
YYYR
BYBY
BBBY
BBBY
output
Yes
input
7 6
AAAAAB
ABBBAB
ABAAAB
ABABBB
ABAAAB
ABBBAB
AAAAAB
output
Yes
input
2 13
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
output
No
Note

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 ;
}

 

你可能感兴趣的:(codeforces510--A. Fox And Snake--B. Fox And Two Dots)