HDU 5040 BFS+狀壓,hdu5040bfs
2014 ACM/ICPC Asia Regional Beijing Online
對於N*N的矩陣
M起點,T終點
有起始方向分別向北N,東E,南S,西W的網路攝影機,可以檢測的範圍為自己+所指方向1格,每1秒順時針旋轉90°
前面有燈或者自己站的地方有燈,移動需要花3秒,或者原地等一秒。
BFS優先隊列
開3維 hash數組判重,第三維是在該點等待的時間,開到4即可(網路攝影機轉一圈)
對圖中的每個點提前處理處會在什麼時候被網路攝影機看到,用2進位壓縮儲存在MAP數組中
然後常規BFS解決
比賽時候手殘沒寫CASE。。。。找了半天錯誤。。。。。
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'====== `=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fozubaoyou pass System Test!*/#include "stdio.h"#include "string.h"#include "queue"using namespace std;int inf=0x3f;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};struct node{ int x,y,step,wait; // step記錄步數,wait記錄等待的時間 friend bool operator<(node n1,node n2) { return n1.step>n2.step; }};int s_x,s_y,n;int b[5];char str[510][510]; int map[510][510],hash[510][510][5]; // MAP 儲存每個點什麼時候會被看到,HASH判重第三維是在該點等待的時間int bfs(){ priority_queue<node>q; node cur,next; int i,time; cur.x=s_x; cur.y=s_y; cur.step=0; cur.wait=0; q.push(cur); memset(hash,inf,sizeof(hash)); hash[cur.x][cur.y][0]=0; while (!q.empty()) { cur=q.top(); q.pop(); if (str[cur.x][cur.y]=='T') return cur.step; for (i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; if (next.x<0 || next.x>=n || next.y<0 || next.y>=n) continue; if (str[next.x][next.y]=='#') continue; time=b[cur.step%4]; if ((map[cur.x][cur.y]&time)==time || (map[next.x][next.y]&time)==time) // 當前點或下一個點會被看到 next.step=cur.step+3; else next.step=cur.step+1; next.wait=0; if (next.step<hash[next.x][next.y][next.wait]) { q.push(next); hash[next.x][next.y][next.wait]=next.step; } } next.x=cur.x; next.y=cur.y; next.step=cur.step+1; next.wait=cur.wait+1; if (next.wait<=3 && next.step<hash[next.x][next.y][next.wait]) // 原地等待 { q.push(next); hash[next.x][next.y][next.wait]=next.step; } } return -1;}int main(){ int Case,ii,i,j; scanf("%d",&Case); b[0]=1; b[1]=2; b[2]=4; b[3]=8; for (ii=1;ii<=Case;ii++) { scanf("%d",&n); getchar(); for (i=0;i<n;i++) gets(str[i]); printf("Case #%d: ",ii); memset(map,0,sizeof(map)); for (i=0;i<n;i++) // 狀壓儲存每個點什麼時候會被看到 for (j=0;j<n;j++) { if (str[i][j]=='M') { s_x=i; s_y=j;} if (str[i][j]=='N') { map[i][j]=15; if (i-1>=0) map[i-1][j]|=1; if (j+1<n) map[i][j+1]|=2; if (i+1<n) map[i+1][j]|=4; if (j-1>=0) map[i][j-1]|=8; } if (str[i][j]=='E') { map[i][j]=15; if (j+1<n) map[i][j+1]|=1; if (i+1<n) map[i+1][j]|=2; if (j-1>=0) map[i][j-1]|=4; if (i-1>=0) map[i-1][j]|=8; } if (str[i][j]=='S') { map[i][j]=15; if (i+1<n) map[i+1][j]|=1; if (j-1>=0) map[i][j-1]|=2; if (i-1>=0) map[i-1][j]|=4; if (j+1<n) map[i][j+1]|=8; } if (str[i][j]=='W') { map[i][j]=15; if (j-1>=0) map[i][j-1]|=1; if (i-1>=0) map[i-1][j]|=2; if (j+1<n) map[i][j+1]|=4; if (i+1<n) map[i+1][j]|=8; } } printf("%d\n",bfs()); } return 0;}
hdu 1181 BFS為何WA,大牛指點
/*
給樓主一組資料
bb
tm
0
=========================
有個小bug,改後就AC了
*/
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define N 10020
struct Point
{
char pre,end;
};
Point map[N];
bool vis[N];
int n;
void BFS()
{
int i;
char a;
queue<char> Q;
memset(vis,false,sizeof(vis));
for(i=0;i<n;i++)
if(map[i].pre=='b')
{
Q.push(map[i].end);
}
while(!Q.empty())
{
a=Q.front();
Q.pop();
for(i=0;i<n;i++)
{
if(map[i].pre==a && map[i].end=='m')//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
printf("Yes.\n");
return ;
}
if(!vis[i] && map[i].pre==a)
{
vis[i]=true;
Q.push(map[i].end);
}
}
}
printf("No.\n");
}
int main()
{
char str[102];
n=0;
while(scanf("%s",str)!=EOF)
{
if(str[0]=='0')
{
BFS();
n=0;
continue;
}
map[n].pre=str[0];
map[n].end=str[(int)strlen(str)-1];
n++;
}
return 0;
}...餘下全文>>
hdu hdoj 1010 可以不可以用BFS?
應該是可以的。。隊列結構體,一個座標,一個時間。。
當時間相等 並且座標相等時,YES。
當隊列頭的時間大於T,NO。
米寫過不知是否可行。。
0MS代碼。。
#include <stdio.h>
#include <string.h>
const int dir[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};
const int MAX=8;
int n,m;
char map[MAX][MAX];
int dx,dy;
struct
{
int x,y;
} q[1000000];
int used[MAX][MAX];
int vabs(int x)
{
return x<0?-x:x;
}
int dis(int x,int y)
{
return vabs(x)+vabs(y);
}
int BFS(int x,int y) //先判斷是否有路徑
{
int tx,ty,i,f=-1,r=0;
memset(used,0,sizeof(used));
used[x][y]=1;
q[0].x=x;
q[0].y=y;
while(f!=r){
f++;
x=q[f].x;
y=q[f].y;
for(i=0; i<4; i++){
tx = x+dir[i][0];
ty = y+dir[i][1];
if(tx==dx && ty==dy) return 1;
if(tx<0||tx>=n||ty<0||ty>=m||used[tx][ty]) continue;
if(map[tx][ty]=='X') continue;
used[tx][ty]=1; //對點做標記
++r;
q[r].x=tx;
q[r].y=ty;
}
}
return 0;
}
int able(int x,int y)
{
memset(used,0,sizeof(used));
int i,f=-1,r=0,tx,ty,cnt=0;
q[0].x=x;
q[0].y=y;
used[x][y]=1;
while(f!=r){
f++;
x=q[f].x;
y=q[f].y;
for(i=0; i<4; i++){
tx=dir[i][0]+x;
ty=dir[i][1]+y;
if(tx<0||tx>=n||ty<0||ty>=m||used[tx][ty]) continue;
if(map[tx][ty]=='X') continue;
cnt++;
used[tx][ty]=1;
r++;
q[r......餘下全文>>