HDU 2717
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
INPUT
Line 1: Two space-separated integers: N and K
OUTPUT
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int ans,n,k;
int time[100050];
using namespace std;
void bfs()
{
queue<int>que;
que.push(n);
time[n]=0; int x,y;
while(!que.empty())
{
x=que.front();que.pop();
if(x==k)return;
y=x+1;
if(y>=0&&y<=100000&&time[y]==0){
//BFS下第一个到达该点的路径就是最小消耗路径
//每个点只进行第一次更新
time[y]=time[x]+1;que.push(y);
}
y=x-1;
if(y>=0&&y<=100000&&time[y]==0){
time[y]=time[x]+1;que.push(y);
}
y=2*x;//tp的方式不可放在最前,因为0*2还是0,却错误的把时间更新为1
if(y>=0&&y<=100000&&time[y]==0){
time[y]=time[x]+1;que.push(y);
}
}
}
int main()
{
// freopen("datain.txt","r",stdin);
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(time,0,sizeof(time));
bfs();
printf("%d\n",time[k]);
}
return 0;
}
HDU2102
公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
int x,y,z,val;
};
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
char gra[2][35][35];
int vis[2][35][35];
int n,m,T,flag;
int judge(node next)
{
if(next.x<0||next.x>=n||next.y<0||next.y>=m)return 0;
if(vis[next.z][next.x][next.y]==1)return 0;
if(gra[next.z][next.x][next.y]=='*')return 0;
if(gra[next.z][next.x][next.y]=='#'&&gra[!next.z][next.x][next.y]=='*')return 0;
if(gra[next.z][next.x][next.y]=='#'&&gra[!next.z][next.x][next.y]=='#')return 0;
return 1;
}
void bfs(node st)
{
queue<node>q;
q.push(st);
vis[0][0][0]=1;
node now,next;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.val>T)return;
if(gra[now.z][now.x][now.y]=='P')
{
printf("YES\n");
flag=1;
return;
}
for(int i=0; i<4; i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.z=now.z;
if(judge(next))
{
if(gra[next.z][next.x][next.y]=='#')
{
vis[next.z][next.x][next.y]=1;
next.z=!next.z;
}
next.val=now.val+1;
vis[next.z][next.x][next.y]=1;
q.push(next);
}
}
}
}
int main()
{
// freopen("datain.txt","r",stdin);
int c;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&m,&T);
memset(vis,0,sizeof(vis));
flag=0;
for(int i=0; i<n; i++)
{
scanf("%s",gra[0][i]);
}
for(int i=0; i<n; i++)
{
scanf("%s",gra[1][i]);
}
node st;
st.z=0;//该题起点固定为(0,0,0)
st.x=0;
st.y=0;
st.val=0;
bfs(st);
if(!flag)printf("NO\n");
}
}
HDU1180
Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
Input
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
Output
只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
# include<iostream>
# include<queue>
using namespace std;
char maze[22][22];
int visited[22][22];
int waited[22][22];
int m,n,startx,starty,endx,endy;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
typedef struct Point{
int x,y,time;//横坐标x,纵坐标y,时间time
bool operator <(const Point &p) const{
return time>p.time;//最小值优先
}
}Point;
priority_queue<Point> q;
int bfs();
int main()
{
while(cin>>m>>n)
{
if(m==0&&n==0)
continue;
int i,j;
memset(visited,0,sizeof(int)*22*22);
memset(waited,0,sizeof(int)*22*22);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='S')
{
startx=i;
starty=j;
}
else if(maze[i][j]=='T')
{
endx=i;
endy=j;
}
}
}
printf("%d\n",bfs());
}
return 0;
}
int bfs()
{
Point t,nt;
t.x=startx;
t.y=starty;
t.time=0;
q.push(t);
visited[startx][starty]=1;
while(!q.empty())
{
t=q.top();
q.pop();
if(t.x==endx&&t.y==endy)
return t.time;
int nx,ny,k;
for(k=0;k<4;k++)
{
nx=t.x+dir[k][0];
ny=t.y+dir[k][1];
if(nx>=0&&nx<m&&ny>=0&&ny<n&&maze[nx][ny]!='*'&&!visited[nx][ny])
{
if(maze[nx][ny]=='.'||maze[nx][ny]=='T')
{
nt.x=nx;
nt.y=ny;
nt.time=t.time+1;
visited[nx][ny]=1;
q.push(nt);
}
//待扩展的节点为楼梯
else if(maze[nx][ny]=='|'||maze[nx][ny]=='-')
{
//往左右方向扩展
if(k/2)//dir[0][1]或dir[0][-1]
{
//此时楼梯方向为'-',与扩展方向一致,时间加一既可
if((maze[nx][ny]=='-'&&!(t.time%2))||(maze[nx][ny]=='|'&&t.time%2))
{
if(ny>=-dir[k][1]&&ny<n-dir[k][1]&&maze[nx][ny+dir[k][1]]!='*'&&!visited[nx][ny+dir[k][1]])
{
nt.x=nx;
nt.y=ny+dir[k][1];
visited[nt.x][nt.y]=1;
nt.time=t.time+1;
q.push(nt);
}
}
else if(!waited[t.x][t.y])
{
nt.x=t.x;
nt.y=t.y;
visited[nt.x][nt.y]=1;
waited[nt.x][nt.y]=1;
nt.time=t.time+1;//在原地等一分钟
q.push(nt);
}
}
//上下方向扩展 dir[1][0] dir[-1][0]
else
{
//楼梯方向为'|',扩展方向与楼梯方向一致
if((maze[nx][ny]=='|'&&!(t.time%2))||(maze[nx][ny]=='-'&&t.time%2))
{
if(nx>=-dir[k][0]&&nx<m-dir[k][0]&&maze[nx+dir[k][0]][ny]!='*'&&!visited[nx+dir[k][0]][ny])
{
nt.x=nx+dir[k][0];
nt.y=ny;
visited[nt.x][nt.y]=1;
nt.time=t.time+1;
q.push(nt);
}
}
else if(!waited[t.x][t.y])
{
nt.x=t.x;
nt.y=t.y;
visited[nt.x][nt.y]=1;
waited[nt.x][nt.y]=1;
nt.time=t.time+1;
q.push(nt);
}
}
}
}
}
}
return 0;
}