BFS DFS (2) 走迷宫 炸弹人
来源:互联网 发布:第一初恋网络剧第一季 编辑:程序博客网 时间:2024/05/21 15:43
1.走迷宫
迷宫的行n和列m不超过50
广度优先搜索(Breadth First Search, BFS)
用队列模拟这个过程
#include<stdio.h>struct note{ int x; int y; int s;};int main(){ struct note que[2501]; int book[51][51]={0}; int a[6][5] = { {0,0,0,0,0}, {0,0,0,1,0}, {0,0,0,0,0}, {0,0,0,1,0}, {0,0,1,0,0}, {0,0,0,0,1}}; int startx,starty,p,q; int head,tail,tx,ty,k; int flag = 0; int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //设置起点和终点 do{ printf("请输入起点和终点 如 1 1 4 3表示起点为(1,1),终点为(4,3):\n"); scanf("%d %d %d %d",&startx,&starty,&p,&q); }while(startx < 1 || startx > 50 || starty < 1 || starty > 50 || p < 1 || p > 50 || q < 1 || q > 50); //初始化队列 head = 1; tail = 1; que[tail].x = startx; que[tail].y = starty; que[tail].s = 0; book[startx][starty] = 1; tail ++; //BFS while(head < tail){ for(k = 0; k<=3; k++){ tx = que[tail].x + next[k][0]; ty = que[tail].y + next[k][1]; if(tx < 1 || tx > 50 || ty < 1 || ty > 50){ continue; } if(a[tx][ty] == 0 && book[tx][ty] == 0){ book[tx][ty] = 1; que[tail].x = tx; que[tail].y = ty; que[tail].s = que[head].s + 1; tail++; } if(tx == p && ty == q){ flag = 1; break; } } if(flag = 1){ break; } head++; } printf("从起点到终点要用 %d 步。", que[tail-1].s); getchar(); getchar(); return 0;}
2.炸弹人
找出哪些点是炸弹人可以到达的。可以用BFS或DFS来枚举出所有小人可以到达的点,然后在这些可以到达的点上分别统计可以消灭的敌人数。
2.0 某个位置消灭敌人个数
int get_num(int x, int y){ int sum,i,j; i = x; j = y; //上 while(a[i[j] != '#'){ if(a[i][j] == 'G'){ sum++; } x--; } //下 i = x; j = y; while(a[i[j] != '#'){ if(a[i][j] == 'G'){ sum++; } x++; } //左 i = x; j = y; while(a[i[j] != '#'){ if(a[i][j] == 'G'){ sum++; } y--; } //右 i = x; j = y; while(a[i[j] != '#'){ if(a[i][j] == 'G'){ sum++; } y++; } return sum;}
2.1 广度优先搜索
struct note{ int x; int y;};char a[21][21];int main(){ int book[21][21]; struct note que[401]; int head,tail; int i,j,k,sum,maxNum = 0,maxX,maxY,n,m,startx,starty,tx,ty; int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //右下左上 //地图初始化 scanf("%d %d %d %d",&n,&m,&startx,&starty); for(i=1;i<=n;i++) scanf("%s",&a[i]); //队列初始化 head = 1; tail = 1; que[tail].x = startx; que[tail].y = starty; tail++; book[startx][starty] = 1; maxNum = get_num(startx,starty); maxX = startx; maxY = starty; while(head < tail){ for(k=0;k<=3;k++){ tx = que[head].x + next[k][0]; ty = que[head].y + next[k][1]; //判断越界 if(tx < 1 || tx > n || ty < 1 || ty > m) continue; if(a[tx][ty] == '.' && book[tx][ty] == 0){ book[tx][ty] = 1; que[tail].x = tx; que[tail].y = ty; tail++; sum = get_num(tx,ty); if(sum > maxNum){ maxNum = sum; maxX = tx;maxY = ty; } } } head++; } printf("将炸弹放置在(%d,%d)处,可以消灭%d个敌人。\n",maxX,maxY,maxNum); getchar(); return 0;}
2.2 深度优先搜索
void bomb_dfs(int x,int y){ int sum,mx,my,k,tx,ty; book[x][y] = 1; sum = get_num(x,y); if(sum > maxNum){ maxX = mx; maxY = my; maxNum = sum; } for(k = 0;k <= 3;k++){ tx = x + next[k][0]; ty = y + next[k][1]; //判断越界 if(tx < 1 || tx > n || ty < 1 || ty > m) continue; if(a[tx][ty] == '.' && book[tx][ty]==0){ book[tx][ty] = 1; bomb_dfs(tx, ty); } } return ;}int main(){ ... bomb_dfs(startx,starty); printf("将炸弹放置在(%d,%d)处,可以消灭%d个敌人。\n",maxX,maxY,maxNum); getchar(); return 0;}
0 0
- BFS DFS (2) 走迷宫 炸弹人
- 3752:走迷宫 bfs dfs
- DFS/BFS(解决炸弹人)
- 啊哈炸弹人(dfs bfs)
- 走迷宫(Dfs)
- SDUT 走迷宫(DFS)
- 自动走迷宫(DFS)
- 走迷宫sdut1269(附带dfs求所有路径与bfs求最佳路径)
- nyoj-306-走迷宫(bfs)
- BFS走迷宫
- 走迷宫 bfs
- BFS 走迷宫代码
- BFS 走迷宫
- BFS走迷宫
- 走迷宫最短步数--BFS
- bfs走迷宫
- 栈实验走迷宫(DFS)
- NYOJ 306 走迷宫(DFS+二分法)
- 【转帖】Facebook 的自动化测试
- C++11系列-lambda函数
- 安卓应用每次从后台切换到前台都会启动欢迎界面的问题
- 对象的三种状态
- 仿QQ获取手机中的APK并分享
- BFS DFS (2) 走迷宫 炸弹人
- matlab2014a中help与参考页翻译 Logical Operators: Short-Circuit && ||
- 我的数组类
- 怎么把word转换成pdf文档格式
- oracle sequence跳号 并且回跳
- GCD
- C++11系列-区间迭代
- PHP(FastCGI)在Nginx的alias下404的解决方案
- 电脑警报声大全