迷宫
来源:互联网 发布:刺客信条2mac 编辑:程序博客网 时间:2024/04/28 23:23
问题:http://icpc.njust.edu.cn/Problem/Local/1032/
迷宫
Description
设计一个程序,求出走出迷宫的所需要的最少的步数(可以走八个方向)。其中入口用s表示,出口用e表示。墙用1表示,空格用0表示。输入:输入测试组数t<10;
Input
输入测试组数t<10;输入迷宫大小m行,n列(m,n<100)输入迷宫形状,用s表示入口,e表示出口,一行一行的输
Output
一个数字x,表示最少要x步到达终点
Sample Input
12 2s10e
Sample Output
1
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <queue>using namespace std;int a[100][100];int vist[100][100];typedef struct node{ int i,j,step;}node;int mov[8][2]={{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1}};int main(){ int x; char c;// for(int ii=0;ii<8;ii++)// {// cout<<mov[ii][0]<<" "<<mov[ii][1]<<endl;// } cin>>x; for(int k=0;k<x;k++) { int flag=0,step=0; memset(a,0,sizeof(a)); memset(vist,0,sizeof(vist)); int m,n,start_i=-1,start_j=-1,end_i=-1,end_j=-1; cin>>m>>n; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>c; if(c=='s') { start_i=i; start_j=j; a[i][j]=0; } else if(c=='e') { end_i=i; end_j=j; a[i][j]=0; } else { a[i][j]=c-'0'; } } } // cout<<start_i<<" "<<end_i<<endl; //遍历 queue<node> q; node p,p2; p.i=start_i; p.j=start_j; p.step=0; q.push(p); //cout<<p.i<<endl; while(!q.empty()) { p=q.front(); q.pop(); int i=p.i,j=p.j; // cout<<"i= "<<i<<" j= "<<j<<endl; vist[i][j]=1; if(p.i==end_i && p.j==end_j) { flag=1; step=p.step; break; } for(int ii=0;ii<8;ii++) { if((i+mov[ii][0]>=0) &&(i+mov[ii][0]<m)&&(j+mov[ii][1]>=0) &&(j+mov[ii][1]<n)&&(vist[i+mov[ii][0]][j+mov[ii][1]]!=1) &&a[i+mov[ii][0]][j+mov[ii][1]]!=1) { p2.i=i+mov[ii][0]; p2.j=j+mov[ii][1]; p2.step=p.step+1; q.push(p2); //cout<<"shang"<<endl; } } } if(flag) cout<<step<<endl; else cout<<"-1"<<endl; }}
0 0
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫?
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫。。。
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- Linux系统值得一看的学习方法及路线图
- 页面树形结构
- Leetcode-473. Matchsticks to Square
- 最小重量机器设计问题
- angular指令的transclude选项以及ng-transclude指令
- 迷宫
- C语言概览
- salesforce中常用技能总结(纯粹干货,深度积累)
- 四大组件复习(三)
- 请教定时执行任务问题
- 所谓更牛,就是换个罪受!——《时间的朋友2016跨年演讲》深入笔记
- 一次MAT分析logstash内存溢出
- 怎么在图片窗口后台要增加alt 标签和 title 标签输入框。并在栏目页面alt 标签和 title 标签显示出来?
- 4.图像的空间域变换操作