nyoj-----284坦克大战(带权值的图搜索)
2014-06-11 06:34
218 查看
坦克大战
时间限制:1000 ms | 内存限制:65535 KB难度:3
描述
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.
What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<cstdlib> using namespace std; const int maxn=302; struct node { int val; int minc; }; node map[maxn][maxn]; //先采用深度搜索 int dir[4][2]= { {0,1}, //向右 {-1,0}, //向左 {1,0}, //向上 {0,-1} //向下 }; int m,n,sx,sy,ex,ey; void dfs(int x,int y) { //剪枝 if(x>0&&y>0&&x<=m&&y<=n) { for(int i=0;i<4;i++) { if(map[x+dir[i][0]][y+dir[i][1]].val>0&&map[x+dir[i][0]][y+dir[i][1]].minc>(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val)) //非智能 shit { map[x+dir[i][0]][y+dir[i][1]].minc=(map[x][y].minc+map[x+dir[i][0]][y+dir[i][1]].val); dfs(x+dir[i][0],y+dir[i][1]); } } } } int main() { int i,j; char ss; // freopen("test.in","r",stdin); while(scanf("%d%d",&m,&n)!=EOF&&n+m) { getchar(); memset(map,0,sizeof(map)); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { map[i][j].minc=0x3f3f3f3f; scanf("%c",&ss); if(ss=='E') map[i][j].val=1; else if(ss=='B') map[i][j].val=2; else if(ss=='Y') { map[i][j].minc=0; sx=i; sy=j; } else if(ss=='T') { map[i][j].val=1; ex=i; ey=j; } else map[i][j].val=-1; //代表障碍物 } getchar(); } //algorithm functiom dfs(sx,sy); if(map[ex][ey].minc==0x3f3f3f3f) printf("-1\n"); else printf("%d\n",map[ex][ey].minc); } return 0; }
View Code
当然还可以用bfs来做,时间会更快什么的........
相关文章推荐
- nyoj284-坦克大战(搜索 bfs)
- NYOJ 284 坦克大战
- nyoj 284 坦克大战 简单搜索
- NYOJ - 284 - 坦克大战(BFS求最短路,优先队列)
- 坦克大战(nyoj284)
- nyoj 284 坦克大战 简单搜索
- nyoj-284-坦克大战
- nyoj 284 坦克大战
- nyoj--284--坦克大战(bfs模板)
- NYOJ 搜索题目汇总 NYOJ 20、21、27、42、58、82、202、284、325、353、488、491、523、592、722
- nyoj 284 坦克大战
- NYOJ-284-坦克大战(BFS)
- NYOJ 坦克大战(宽度优先搜索)
- NYOJ-284 坦克大战
- nyoj--284--坦克大战(bfs模板)
- nyoj 284 坦克大战(广搜+优先队列)
- Nyoj 284 坦克大战
- NYOJ 284 坦克大战
- NYOJ-284坦克大战(优先队列)
- NYOJ 284 坦克大战(BFS)