Seeding【dfs】
2015-08-05 08:28
169 查看
Seeding
Time Limit: 2 Seconds
Memory Limit: 65536 KB
It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares.
Tom has a seeding-machine. At the beginning, the machine lies in the top left corner of the field. After the machine finishes one square, Tom drives it into an adjacent square, and continues seeding. In order to protect the machine, Tom will not drive it
into a square that contains stones. It is not allowed to drive the machine into a square that been seeded before, either.
Tom wants to seed all the squares that do not contain stones. Is it possible?
Input
The first line of each test case contains two integers n and m that denote the size of the field. (1 < n, m < 7) The next n lines give the field, each of which contains m characters. 'S' is a square with stones, and '.' is a square without stones.
Input is terminated with two 0's. This case is not to be processed.
Output
For each test case, print "YES" if Tom can make it, or "NO" otherwise.
Sample Input
4 4
.S..
.S..
....
....
4 4
....
...S
....
...S
0 0
Sample Output
YES
NO
题意:问能否找到一条路不走重复路走完整个田地dfs
Time Limit: 2 Seconds
Memory Limit: 65536 KB
It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares.
Tom has a seeding-machine. At the beginning, the machine lies in the top left corner of the field. After the machine finishes one square, Tom drives it into an adjacent square, and continues seeding. In order to protect the machine, Tom will not drive it
into a square that contains stones. It is not allowed to drive the machine into a square that been seeded before, either.
Tom wants to seed all the squares that do not contain stones. Is it possible?
Input
The first line of each test case contains two integers n and m that denote the size of the field. (1 < n, m < 7) The next n lines give the field, each of which contains m characters. 'S' is a square with stones, and '.' is a square without stones.
Input is terminated with two 0's. This case is not to be processed.
Output
For each test case, print "YES" if Tom can make it, or "NO" otherwise.
Sample Input
4 4
.S..
.S..
....
....
4 4
....
...S
....
...S
0 0
Sample Output
YES
NO
题意:问能否找到一条路不走重复路走完整个田地dfs
#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; char map[10][10]; int n,m; int mov[][2]={1,0,-1,0,0,1,0,-1}; int vis[10][10]; int MAX(int x,int y) { return x>y?x:y; } bool judge(int xx,int yy) { if(!vis[xx][yy]&&xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')return true; return false; } int max; void dfs(int x,int y,int s) { max=MAX(s,max); for(int i=0;i<4;++i) { int xx=x+mov[i][1]; int yy=y+mov[i][0]; if(judge(xx,yy)) { vis[xx][yy]=1; dfs(xx,yy,s+1); vis[xx][yy]=0; } } } int main() { int i,j; while(scanf("%d%d",&n,&m)) { if(n==0&&m==0)break; int stone=0; for(i=0;i<n;++i) { scanf("%s",map[i]); for(j=0;j<m;++j) { if(map[i][j]=='S')stone++; } }max=0;memset(vis,0,sizeof(vis)); vis[0][0]=1; dfs(0,0,1); if(max+stone==n*m) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- UI之窗口与视图
- android sdk开发国内下下载地址,超全。。。
- 递归 Lotto
- OA系统如何控制每个人的权限?
- for_each可能导致的数据丢失
- 我的第一个UI程序
- hdu 5348 搜索
- PHP数组合并+与array_merge的区别分析
- Eclipse下svn的创建分支/合并/切换使用
- POJ1014Dividing【多重背包+二进制优化】
- hdoj 5074 Hatsune Miku 【DP】
- cisco syslog配置
- swift protocol
- HTML网页跳转
- SCNetworkReachability 使用
- 利用tcpdump抓包工具监控TCP连接的三次握手和断开连接的四次挥手
- svchost.exe占用cpu过高怎么处理(原因及解决办法)
- ORACLE AUDIT
- OA开发很简单,OA实施很复杂!
- Android 平台下使用 i2c-tools