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 91
Queue 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 91
Queue 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 91
Queue 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;
}