ZOJ 2110 Tempter of the Bone
来源:互联网 发布:智能语音互动软件 编辑:程序博客网 时间:2024/06/08 17:54
比赛的时候写的
#include<stdio.h>#include<string.h>int flag,sx,sy,ex,ey,num;int n,m,t,vis[10][10];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};char map[10][10];int abs(int p){ return p>=0?p:-p;}void dfs(int x,int y,int sum){ int i,xx,yy; if(flag==1) return; if(x==ex&&y==ey&&sum==t) { flag=1; return; } int mindis=abs(x-ex)+abs(y-ey); if(mindis>t-sum||(t-sum-mindis)%2==1) return; for(i=0;i<4;i++) { xx=x+dx[i]; yy=y+dy[i]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X') { vis[xx][yy]=1; dfs(xx,yy,sum+1); vis[xx][yy]=0; } }}int main(){ int i,j; while(~scanf("%d%d%d",&n,&m,&t)) { if(n==0&&m==0&&t==0) break; num=0; for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='D') { ex=i; ey=j; } if(map[i][j]=='X') num++; } } if(n*m-num-1<t) { printf("NO\n"); continue; } flag = 0; memset(vis, 0, sizeof(vis)); vis[sx][sy] = 1; dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
后来看到的
#include<iostream>#include<cstdio>using namespace std;char c[10][10];int n,m;int ok=0;int fx,fy;void dfs(int x,int y,int t){ int temp=t-abs(fx-x)-abs(fy-y); if(temp<0||temp&1) return ; if (c[x][y] != '.' && c[x][y] != 'S') return ; if(t==1) { if(x-1>=0&&c[x-1][y]=='D') {ok=1;return ;} if(x+1<n&&c[x+1][y]=='D') {ok=1;return ;} if(y-1>=0&&c[x][y-1]=='D') {ok=1;return ;} if(y+1<m&&c[x][y+1]=='D') {ok=1;return ;} } else{ c[x][y]='X'; if(x-1>=0&&c[x-1][y]=='.') dfs(x-1,y,t-1); if(x+1<n&&c[x+1][y]=='.') dfs(x+1,y,t-1); if(y-1>=0&&c[x][y-1]=='.') dfs(x,y-1,t-1); if(y+1<m&&c[x][y+1]=='.') dfs(x,y+1,t-1); c[x][y]='.'; }}int main(){ int t; while(cin>>n>>m>>t&&(n||m||t)) { int x=0,y=0; ok=0; for(int i=0;i<n;i++) { scanf("%s",c[i]); for(int j=0;j<m;j++){ if(c[i][j]=='S') x=i,y=j; if(c[i][j]=='D') fx=i,fy=j; } } dfs(x,y,t); if(ok) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
0 0
- zoj 2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- Zoj 2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- ZOJ:2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- ZOJ - 2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- ZOJ 2110 Tempter of the Bone
- ZOJ Tempter of the Bone
- ZOJ-2110 Tempter of the Bone( 搜索,奇偶性剪枝)
- zoj 2110(hdu 1010)Tempter of the Bone(深搜)
- zoj 2110 || hdoj 1010 Tempter of the Bone(DFS)
- ZOJ 2110 Tempter of the Bone DFS搜索+奇偶剪枝
- ZOJ 2110 Tempter of the Bone 搜索 DFS 解法
- ZOJ 2110 Tempter of the Bone (DFS+减枝)
- ZOJ Problem Set - 2110 Tempter of the Bone
- 数据库中的QueryRunner的大致实现
- ubuntu12.04打字软件
- webService学习9:jquery ajax调用webservice
- mysql行变列
- Java (数组的遍历,for循环的使用)
- ZOJ 2110 Tempter of the Bone
- bzoj2333: [SCOI2011]棘手的操作
- 阻碍新手程序员提升的8件小事
- 勾股定理一日一证连载20
- 《CSS权威指南》学习记录——列表
- 15个Android 通用流行框架,需要时翻出来
- 20150421、20150422指针数组
- 浅析C++继承与派生
- Linux安装ftp组件