NYOJ 284
来源:互联网 发布:怎么让淘宝信誉高起来 编辑:程序博客网 时间:2024/06/03 17:20
NYOJ 284
法一:
#include<iostream>#include<cstdio>#include<queue>using namespace std;typedef struct node{ int x,y,step;}node;bool operator<(const node &a,const node &b){ return a.step>b.step;}int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int main(){ char map[305][305]; int i,j,ans,n,m; node a,b; priority_queue<node>q; while(scanf("%d %d",&n,&m)&&(n||m)) { a.x=0; for(i=0;i<n;i++) { scanf("%s*c",&map[i]); for(j=0;j<m;j++) { if(map[i][j]=='Y') { a.x=i; a.y=j; a.step=0; q.push(a); } } } bool flag=false; while(!q.empty())//广搜 { a=q.top(); q.pop(); for(i=0;i<4;i++) { b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x>=0 && b.x<n && b.y>=0 && b.y<m) { if(map[b.x][b.y]=='T') { ans=a.step+1; flag=true; break; } else if(map[b.x][b.y]=='E') { b.step=a.step+1; q.push(b); map[b.x][b.y]='S'; } else if(map[b.x][b.y]=='B') { b.step=a.step+2; q.push(b); map[b.x][b.y]='S'; } } } if(flag) break; } if(flag) printf("%d\n",ans); else printf("-1\n"); while(!q.empty()) { q.pop(); } } return 0;}
法二:
#include <cstdio>#include <cstring>#include <queue>using namespace std;int m, n;struct node{ int x, y; node(int a, int b){ x = a; y = b; }}*xm, *d;char maze[301][301];int dist[301][301], steps;bool vis[301][301];queue<node*> q;void PUSH(int a, int b){ if(vis[a][b] || a < 1 || b < 1 || a > m || b > n || maze[a][b] == 'S' || maze[a][b] == 'R') return; q.push(new node(a, b)); dist[a][b] = steps+1; vis[a][b] = 1;}int bfs(){ memset(vis, 0, sizeof(vis)); memset(dist, 0, sizeof(dist)); node *cur = new node(xm->x, xm->y); while(!q.empty()) q.pop(); q.push(cur); vis[xm->x][xm->y] = 1; while(!q.empty()){ cur = q.front(); q.pop(); steps = dist[cur->x][cur->y]; if(cur->x == d->x && cur->y == d->y) return dist[d->x][d->y]; if(maze[cur->x][cur->y] == 'B'){ maze[cur->x][cur->y] = 'E'; vis[cur->x][cur->y] = 0; PUSH(cur->x, cur->y); }else if(maze[cur->x][cur->y] != 'R' || maze[cur->x][cur->y] != 'S'){ PUSH(cur->x - 1, cur->y); PUSH(cur->x, cur->y - 1); PUSH(cur->x, cur->y + 1); PUSH(cur->x + 1, cur->y); } } return -1;}int main(){ int i, j; while(scanf("%d%d", &m, &n) && m||n){ for(i = 1; i <= m; i++) for(j = 1; j <= n; j++){ scanf(" %c ", &maze[i][j]); if(maze[i][j] == 'Y') xm = new node(i, j); else if(maze[i][j] == 'T') d = new node(i, j); } printf("%d\n", bfs()); } return 0;}
priority_queue
#include<iostream>#include<functional>#include<queue>#include<vector>using namespace std;//定义比较结构struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值优先 }};struct cmp2{ bool operator ()(int &a,int &b){ return a<b;//最大值优先 }};//自定义数据结构struct number1{ int x; bool operator < (const number1 &a) const { return x>a.x;//最小值优先 }};struct number2{ int x; bool operator < (const number2 &a) const { return x<a.x;//最大值优先 }};int a[]={14,10,56,7,83,22,36,91,3,47,72,0};number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};int main(){ priority_queue<int>que;//采用默认优先级构造队列 priority_queue<int,vector<int>,cmp1>que1;//最小值优先 priority_queue<int,vector<int>,cmp2>que2;//最大值优先 priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误, priority_queue<int,vector<int>,less<int> >que4;////最大值优先 priority_queue<number1>que5; //最小优先级队列 priority_queue<number2>que6; //最大优先级队列 int i; for(i=0;a[i];i++){ que.push(a[i]); que1.push(a[i]); que2.push(a[i]); que3.push(a[i]); que4.push(a[i]); } for(i=0;num1[i].x;i++) que5.push(num1[i]); for(i=0;num2[i].x;i++) que6.push(num2[i]); printf("采用默认优先关系:/n(priority_queue<int>que;)/n"); printf("Queue 0:/n"); while(!que.empty()){ printf("%3d",que.top()); que.pop(); } puts(""); puts(""); printf("采用结构体自定义优先级方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n"); printf("Queue 1:/n"); while(!que1.empty()){ printf("%3d",que1.top()); que1.pop(); } puts(""); printf("Queue 2:/n"); while(!que2.empty()){ printf("%3d",que2.top()); que2.pop(); } puts(""); puts(""); printf("采用头文件/"functional/"内定义优先级:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n"); printf("Queue 3:/n"); while(!que3.empty()){ printf("%3d",que3.top()); que3.pop(); } puts(""); printf("Queue 4:/n"); while(!que4.empty()){ printf("%3d",que4.top()); que4.pop(); } puts(""); puts(""); printf("采用结构体自定义优先级方式二:/n(priority_queue<number>que)/n"); printf("Queue 5:/n"); while(!que5.empty()){ printf("%3d",que5.top()); que5.pop(); } puts(""); printf("Queue 6:/n"); while(!que6.empty()){ printf("%3d",que6.top()); que6.pop(); } puts(""); return 0;}/*运行结果 :采用默认优先关系:(priority_queue<int>que;)Queue 0:83 72 56 47 36 22 14 10 7 3采用结构体自定义优先级方式一:(priority_queue<int,vector<int>,cmp>que;)Queue 1: 7 10 14 22 36 47 56 72 83 91Queue 2:83 72 56 47 36 22 14 10 7 3采用头文件"functional"内定义优先级:(priority_queue<int,vector<int>,greater<int>/less<int> >que;)Queue 3: 7 10 14 22 36 47 56 72 83 91Queue 4:83 72 56 47 36 22 14 10 7 3采用结构体自定义优先级方式二:(priority_queue<number>que)Queue 5: 7 10 14 22 36 47 56 72 83 91Queue 6:83 72 56 47 36 22 14 10 7 3*/
法三:
#include <iostream> #include <queue> #include <cstdio> #include <cstring> using namespace std; #define max 301 char map[max][max]; int n,m; bool vist[max][max]; int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; struct node { int x,y,c; friend bool operator <(const node &s1,const node &s2) { return s1.c>s2.c; } }; node s,e; int bfs() { priority_queue < node > q; memset(vist,0,sizeof(vist)); node now,t; s.c=0; q.push(s); vist[s.x][s.y]=1; while (!q.empty()) { now=q.top(); q.pop(); if (now.x==e.x&&now.y==e.y) { return now.c; } for (int i=0;i<4;i++) { t.x=now.x+d[i][0]; t.y=now.y+d[i][1]; if (t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&!vist[t.x][t.y]) { vist[t.x][t.y]=1; if (map[t.x][t.y]=='B') { t.c=now.c+2; q.push(t); } else if (map[t.x][t.y]=='E'||map[t.x][t.y]=='T') { t.c=now.c+1; q.push(t); } } } } return -1; } int main() { int i,j; while (cin>>n>>m,m+n) { for (i=0;i<n;i++) { for (j=0;j<m;j++) { cin>>map[i][j]; if (map[i][j]=='Y') s.x=i,s.y=j; if (map[i][j]=='T') e.x=i,e.y=j; } } int sum=bfs(); cout<<sum<<endl; } return 0; }
0 0
- NYOJ 284
- nyoj 284
- nyoj-284-坦克大战
- NYOJ-284 坦克大战
- Nyoj 284 坦克大战
- NYOJ-284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战
- NYOJ 284 坦克大战
- NYOJ 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- 微信签到程序-2014毕业五月
- adb常用查看内存信息的命令
- NYOJ 202
- [CODEVS1283]等差子序列解题报告
- openwrt的编译
- NYOJ 284
- ArcEngine-Java中符号选择器(SymbolSelector)的使用
- C++学习笔记(第13章->类继承)
- error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'
- 从头认识C—float类型
- NYOJ 325
- 8、黑马程序员-C语言函数
- maven构建android项目问题备忘
- UVA - 10340 - All in All (字符串处理!)