DFS HDU1010
2016-06-09 22:42
323 查看
#include <iostream> #include"sstream" #include"string" #include"algorithm" #include"cstring" #include"cstdio" using namespace std; int dir[4][2]={1,0,-1,0,0,1,0,-1}; char g[20][20]; int escape; int r,c,t; int si,sj,ei,ej; void dfs(int s,int e,int cnd) { if(s<0||s>=r||e<0||e>=c) return; int temp=(t-cnd)-(abs(ei-s)+abs(ej-e)); if(temp<0||temp%2) return; if(cnd==t&&s==ei&&e==ej) {escape=1;return;} int i; for(i=0;i<4;i++) { int ki,kj; ki=s+dir[i][0]; kj=e+dir[i][1]; if(g[ki][kj]!='X'&&!escape) { g[ki][kj]='X'; dfs(ki,kj,cnd+1); g[ki][kj]='.'; } } } int main() { //freopen("a.txt","r",stdin); while(cin>>r>>c>>t&&(t+c+r)) { int i,j; int wall=0; for(i=0;i<r;i++) for(j=0;j<c;j++) { cin>>g[i][j]; if(g[i][j]=='X') wall++; if(g[i][j]=='D') { ei=i;ej=j; } if(g[i][j]=='S') { si=i;sj=j; } } if(r*c-wall<t) {cout<<"NO\n";continue;} escape=0; g[si][sj]='X'; dfs(si,sj,0); if(escape) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
相关文章推荐
- 114. Flatten Binary Tree to Linked List
- 书上例题 格子游戏
- Tensorflow 安装
- IIS负载均衡
- zigbee协议栈应用(四)广播组网-无线数据传输
- C++虚函数表解析
- pandas+matploblib画图 叠加两张图
- DFS 1241
- 使用Primose方式解决异步编程回调的一些问题--animate动画的例子
- 今天端午节吃了粽子
- driver makefile
- NYOJ 1070 诡异的电梯【Ⅰ】
- 百度贴吧_爬虫
- 入门java script
- 安装RedHat教程
- 113. Path Sum II
- 使用RAII来管理对象资源
- 设计模式——设计模式之禅的阅读笔记
- Docker import error:Error: open /tmp/docker-import-123456789/repo/bin/json: no such file or directory
- 大型网站架构系列:分布式消息队列