B. Fox And Two Dots http://codeforces.com/contest/510/problem/B
简单的DFS
#include<iostream>
#include<string.h>
using namespace std;
char st[55][55];
int vis[55][55] , turn[4][2] ={0,1,0,-1,1,0,-1,0};
int n , m , flag;
void dfs(int x , int y , char ch, int limit)
{
if(flag) return;
vis[x][y] = 1;
for(int i = 0 ; i < 4 ; i++)
{
if(limit == i)
continue;
int xx = turn[i][0] + x;
int yy = turn[i][1] + y;
if(xx >= 0 && yy >= 0 && xx < n && yy < m && st[xx][yy] == ch)
{
if(vis[xx][yy] == 1)
{
flag = 1; return;
}
else
{
if(i == 1)
dfs(xx,yy,ch,0);
if(i == 0)
dfs(xx,yy,ch,1);
if(i == 2)
dfs(xx,yy,ch,3);
if(i == 3)
dfs(xx,yy,ch,2);
}
}
}
}
int main()
{
cin >> n >> m;
for(int i = 0 ; i < n ; i++)
cin >>st[i];
for(int i = 0 ; i < n ;i++)
for(int j = 0 ; j < m ; j++)
{
memset(vis,0,sizeof(vis));
dfs(i,j,st[i][j],-1);
if(flag)
{
cout << "Yes" << endl;
return 0;
}
}
cout << "No" <<endl;
return 0;
}
A. Fox And Snake http://codeforces.com/contest/510/problem/A
签名题 输出蛇
#include<iostream>
using namespace std;
int main()
{
int n , m ;
while(cin >> n >> m)
{
for(int i = 1 ; i <= n ;i++)
{
for(int j = 1 ; j <= m ;j++)
{
if(i%2)
cout << "#" ;
else
{
if(i%4 == 0 && j == 1)
cout << "#" ;
else if(i%2 == 0 && j == m && i%4 !=0)
cout << "#" ;
else
cout << ".";
}
}
cout << endl;
}
}
return 0;
}
本文提供CodeForces B.FoxAndTwoDots问题的解决方案,使用深度优先搜索(DFS)判断棋盘上是否存在由相同字符构成的环。同时介绍了A.FoxAndSnake问题的解答,输出特定的蛇形图案。
614

被折叠的 条评论
为什么被折叠?



