swustoj2521 acmer的约会—状态压缩+bfs
来源:互联网 发布:python 定时发送邮件 编辑:程序博客网 时间:2024/05/20 20:18
Description
一个acmer想要去和妹子约会,他们处于一个N行M列的方格地图中,acmer在地图左上角坐标为(1,1)的位置,而妹子在地图右下角坐标为(n,m)的位置,acmer一个单位时间只可以移动到上下左右相邻的一个格子上。然而在这个地图中有许多墙和不同颜色的门,墙是不可以穿越的,只有拿到和门颜色一样的钥匙,才能打开对应颜色的门,只有两个格子之间的门都被打开了acmer才可以从当前格子走到门另一边的格子。acmer当然想要最快时间内到达妹子的位置,你能帮助acmer计算出最短到达的时间么?
Input
多组测试数据。第一行输入三个整数,N、M、P,分别表示地图有N行M列,P种颜色的门(1 <= N,M <= 50 , 0 <= P <= 10)第二行输入一个整数K,表示有墙和门的数量总共有K个(0 <= K <= 500)接下来K行,每行输入5个整数,xi1,yi1,xi2,yi2,gi。如果gi值为0,表示格子(xi1,yi1)和格子(xi2,yi2)之间有墙,如果gi>=1,表示格子(xi1,yi1)和格子(xi2,yi2)之间有一个gi颜色的门。接下来输入一个整数S,表示有数量为S的钥匙在迷宫中(0 <= S <= 50)接下来S行,每行输入3个整数,xi1,yi1,qi,表示格子(xi1,yi1)含有颜色为qi的钥匙。1 <= xi1,xi2 <= N 1 <= yi1,yi2 <= M | xi1 - xi2 | + | yi1 - yi2 |=1 0 <= gi <= P 1 <= qi <= P
Output
输出一个整数表示acmer最短到达妹子位置的时间,如果acmer不能到达妹子的位置,则输出"zhuding gudu yisheng"(引号不用输出)
4 4 991 2 1 3 21 2 2 2 02 1 2 2 02 1 3 1 02 3 3 3 02 4 3 4 13 2 3 3 03 3 4 3 04 3 4 4 022 1 24 2 1
14
这道题是我们学校校赛的题目,渣校的渣生。。。。555555
这就是一个bfs+状态压缩,这是我写的状态压缩的第二题,其实这道题的难度不是很大,除了普通的状态压缩加bfs之外,我们还要开一个四维的数组记录门的状态;
这里要说明3个坑点,,,1.起点可能有钥匙,2.一个点可能有多个钥匙 3.点与点之间可能有多扇门
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#define nn 53
#define inff 0x3fffffff
#define mod 1000000007
#define eps 1e-9
using namespace std;
struct node
{
int x,y;
int time;
int key;
};
int use[nn][nn][1<<10];
int men[nn][nn][nn][nn];
int ans;
int dir[4][2]={{1,0},
{-1,0},
{0,1},
{0,-1},
};
int n,m;
int tu[nn][nn];
int check(int tx,int ty)
{
if(tx>=1 && ty>=1 && tx<=n && ty<=m )
return 1;
else return 0;
}
queue<node> que;
void bfs(int sx,int sy)
{
node now,pre;
while(!que.empty())
que.pop();
int i,j;
int tx,ty;
now.key=0;
now.key=now.key|tu[sx][sy];
now.time=0;
now.x=sx;
now.y=sy;
use[sx][sy][now.key]=1;
que.push(now);
while(!que.empty())
{
pre=que.front();
que.pop();
if(pre.x==n && pre.y==m)
{
ans=pre.time;
return;
}
for(i=0;i<4;i++)
{
tx=pre.x+dir[i][0];
ty=pre.y+dir[i][1];
now.y=ty;
now.x=tx;
now.key=pre.key;
now.time=pre.time;
if(check(tx,ty))
{
if( men[pre.x][pre.y][tx][ty]==-1 )
continue;
if(men[pre.x][pre.y][tx][ty]!=0)
{
int hh=now.key & men[pre.x][pre.y][tx][ty];
int yy=hh^men[pre.x][pre.y][tx][ty];
if( yy )
continue;
}
now.time++;
if(tu[tx][ty]!=0)
{
int kk=tu[tx][ty];
now.key=now.key|kk;
if(use[tx][ty][now.key]==0)
{
use[tx][ty][now.key]=1;
que.push(now);
}
}
else if(use[tx][ty][now.key]==0)
{
use[tx][ty][now.key]=1;
que.push(now);
}
}
}
}
}
int main()
{
int i,j;
int p;
int s,k;
int x1,x2,y1,y2,g,q1;
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
tu[i][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
for(int y=1;y<=n;y++)
for(int o=1;o<=m;o++)
men[i][j][y][o]=0;
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&g);
if(g!=0 && men[x1][y1][x2][y2]!=-1)
{
men[x1][y1][x2][y2]=men[x1][y1][x2][y2]|(1<<g);
men[x2][y2][x1][y1]=men[x2][y2][x1][y1]|(1<<g);
}
else if(g==0)
{
men[x1][y1][x2][y2]=-1;
men[x2][y2][x1][y1]=-1;
}
}
scanf("%d",&s);
for(i=0;i<s;i++)
{
scanf("%d %d %d",&x1,&y1,&q1);
tu[x1][y1]=tu[x1][y1]|(1<<q1);
}
memset(use,0,sizeof(use));
ans=-1;
bfs(1,1);
if(ans==-1)
{
printf("zhuding gudu yisheng\r\n");
}
else printf("%d\r\n",ans);
}
}
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#define nn 53
#define inff 0x3fffffff
#define mod 1000000007
#define eps 1e-9
using namespace std;
struct node
{
int x,y;
int time;
int key;
};
int use[nn][nn][1<<10];
int men[nn][nn][nn][nn];
int ans;
int dir[4][2]={{1,0},
{-1,0},
{0,1},
{0,-1},
};
int n,m;
int tu[nn][nn];
int check(int tx,int ty)
{
if(tx>=1 && ty>=1 && tx<=n && ty<=m )
return 1;
else return 0;
}
queue<node> que;
void bfs(int sx,int sy)
{
node now,pre;
while(!que.empty())
que.pop();
int i,j;
int tx,ty;
now.key=0;
now.key=now.key|tu[sx][sy];
now.time=0;
now.x=sx;
now.y=sy;
use[sx][sy][now.key]=1;
que.push(now);
while(!que.empty())
{
pre=que.front();
que.pop();
if(pre.x==n && pre.y==m)
{
ans=pre.time;
return;
}
for(i=0;i<4;i++)
{
tx=pre.x+dir[i][0];
ty=pre.y+dir[i][1];
now.y=ty;
now.x=tx;
now.key=pre.key;
now.time=pre.time;
if(check(tx,ty))
{
if( men[pre.x][pre.y][tx][ty]==-1 )
continue;
if(men[pre.x][pre.y][tx][ty]!=0)
{
int hh=now.key & men[pre.x][pre.y][tx][ty];
int yy=hh^men[pre.x][pre.y][tx][ty];
if( yy )
continue;
}
now.time++;
if(tu[tx][ty]!=0)
{
int kk=tu[tx][ty];
now.key=now.key|kk;
if(use[tx][ty][now.key]==0)
{
use[tx][ty][now.key]=1;
que.push(now);
}
}
else if(use[tx][ty][now.key]==0)
{
use[tx][ty][now.key]=1;
que.push(now);
}
}
}
}
}
int main()
{
int i,j;
int p;
int s,k;
int x1,x2,y1,y2,g,q1;
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
tu[i][j]=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
for(int y=1;y<=n;y++)
for(int o=1;o<=m;o++)
men[i][j][y][o]=0;
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&g);
if(g!=0 && men[x1][y1][x2][y2]!=-1)
{
men[x1][y1][x2][y2]=men[x1][y1][x2][y2]|(1<<g);
men[x2][y2][x1][y1]=men[x2][y2][x1][y1]|(1<<g);
}
else if(g==0)
{
men[x1][y1][x2][y2]=-1;
men[x2][y2][x1][y1]=-1;
}
}
scanf("%d",&s);
for(i=0;i<s;i++)
{
scanf("%d %d %d",&x1,&y1,&q1);
tu[x1][y1]=tu[x1][y1]|(1<<q1);
}
memset(use,0,sizeof(use));
ans=-1;
bfs(1,1);
if(ans==-1)
{
printf("zhuding gudu yisheng\r\n");
}
else printf("%d\r\n",ans);
}
}
0 0
- swustoj2521 acmer的约会—状态压缩+bfs
- 胜利大逃亡—bfs+状态压缩
- BFS+状态压缩 HDU1429
- hdu1429(BFS+状态压缩)
- 状态压缩+bfs
- HDU_4856_Tunnels(BFS+状态压缩)
- bfs+状态压缩dp
- hdu1429 状态压缩+bfs
- [poj1324]状态压缩+BFS
- 状态压缩+bfs
- hdu4845 状态压缩BFS
- hdu5025(bfs + 状态压缩)
- hdu5094Maze bfs+状态压缩
- 某校赛题(bfs+状态压缩)
- hdu5094 状态压缩+bfs
- POJ1324 状态压缩+BFS
- hdu1429 bfs+状态压缩
- hdu1429 BFS+状态压缩
- HTML5新特性之Communication
- UITabeViews---设置字体格式,大小,颜色
- 用例规约
- Linux tar 解压命令总结
- c# 调用Web Service的问题:The operation 'XXX' could not be loaded because it has a parameter...
- swustoj2521 acmer的约会—状态压缩+bfs
- AutoHotkey数组例子
- linux awk命令详解
- https://leetcode.com/problems/sqrtx/
- 【原创】windows 进程管理
- 位运算符(&【按位与】,|【按位或】,^按位异或,~【按位取反】,<<【向左移动】,>>【向右移动】)
- 第九周 项目二--Time类中的运算符重载(续)
- php 中使用cURL发送get/post请求,上传图片,批处理
- 连接池和使用dbutils