题目链接

给一个n*m的图, 图上有n个标号, n<=4, 然后有墙, 还有一个终点x。 每一步, 只能走某一个标号, 可以向四个方向走, 然后必须要碰到墙或者图的边界或者另一个标号才能停下来。 问你在t步之内能否使第一个标号到达终点。

因为有一个上限t。 所以直接bfs就可以, 感觉思路不是很难, 但是写代码+调试花了超级久...不过总算1A, 不然就懵逼了。

#include <bits/stdc++.h>

using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<11
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = ;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
struct node
{
vector<pll> a;
int step;
node() {
step = ;
}
};
int cnt, w, h;
map <vector<pll>, int> mp;
vector <int> row[], col[];
pll target;
char s[][];
int bfs(const node& init, int t)
{
queue <node> q;
q.push(init);
while(!q.empty()) {
node tmp = q.front(); q.pop();
if(tmp.step > t)
continue;
if(tmp.a[] == target) {
return tmp.step;
}
for(int i = ; i < cnt; i++) {
int x = tmp.a[i].fi, y = tmp.a[i].se;
int tmpy = *(--lower_bound(row[x].begin(), row[x].end(), y));
for(int j = ; j < cnt; j++) {
if(i == j)
continue;
if(tmp.a[i].fi == tmp.a[j].fi && tmp.a[i].se > tmp.a[j].se) {
tmpy = max(tmpy, tmp.a[j].se);
}
}
tmpy++;
vector<pll> ha(tmp.a);
ha[i].se = tmpy;
if(!mp[ha]) {
node newNode = tmp;
newNode.a[i].se = tmpy;
newNode.step++;
q.push(newNode);
mp[ha] = ;
} x = tmp.a[i].fi, y = tmp.a[i].se;
tmpy = *lower_bound(row[x].begin(), row[x].end(), y);
for(int j = ; j < cnt; j++) {
if(i == j)
continue;
if(tmp.a[i].fi == tmp.a[j].fi && tmp.a[i].se < tmp.a[j].se) {
tmpy = min(tmpy, tmp.a[j].se);
}
}
tmpy--;
ha = tmp.a;
ha[i].se = tmpy;
if(!mp[ha]) {
node newNode = tmp;
newNode.a[i].se = tmpy;
newNode.step++;
mp[ha] = ;
q.push(newNode);
} x = tmp.a[i].fi, y = tmp.a[i].se;
int tmpx = *(--lower_bound(col[y].begin(), col[y].end(), x));
for(int j = ; j < cnt; j++) {
if(i == j)
continue;
if(tmp.a[i].se == tmp.a[j].se && tmp.a[i].fi > tmp.a[j].fi) {
tmpx = max(tmpx, tmp.a[j].fi);
}
}
tmpx++;
ha = tmp.a;
ha[i].fi = tmpx;
if(!mp[ha]) {
node newNode = tmp;
newNode.a[i].fi = tmpx;
newNode.step++;
q.push(newNode);
mp[ha] = ;
}
x = tmp.a[i].fi, y = tmp.a[i].se;
tmpx = *lower_bound(col[y].begin(), col[y].end(), x);
for(int j = ; j < cnt; j++) {
if(i == j)
continue;
if(tmp.a[i].se == tmp.a[j].se && tmp.a[i].fi < tmp.a[j].fi) {
tmpx = min(tmpx, tmp.a[j].fi);
}
}
tmpx--;
ha = tmp.a;
ha[i].fi = tmpx;
if(!mp[ha]) {
node newNode = tmp;
newNode.a[i].fi = tmpx;
newNode.step++;
q.push(newNode);
mp[ha] = ;
}
}
}
return -;
}
int main()
{
int t;
while(cin>>cnt>>w>>h>>t) {
mp.clear();
for(int i = ; i < h; i++) {
scanf("%s", s[i]);
}
for(int i = ; i < h; i++) {
row[i].clear();
row[i].pb(-);
row[i].pb(w);
}
for(int i = ; i < w; i++) {
col[i].clear();
col[i].pb(-);
col[i].pb(h);
}
node init;
for(int i = ; i < h; i++) {
for(int j = ; j < w; j++) {
if(s[i][j] == 'X')
target = mk(i, j);
if(s[i][j] == 'W') {
row[i].pb(j);
col[j].pb(i);
}
}
}
for(int i = ; i <= cnt; i++) {
for(int j = ; j < h; j++) {
for(int k = ; k < w; k++) {
if(s[j][k]-'' == i) {
init.a.pb(mk(j, k));
}
}
}
}
for(int i = ; i < h; i++) {
sort(row[i].begin(), row[i].end());
}
for(int i = ; i < w; i++)
sort(col[i].begin(), col[i].end());
mp[init.a] = ;
int ans = bfs(init, t);
if(ans == -) {
puts("NO SOLUTION");
} else {
cout<<ans<<endl;
}
}
}

uvalive 6888 Ricochet Robots bfs的更多相关文章

  1. UVALive 5066 Fire Drill BFS+背包

    H - Fire Drill Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  2. UVALive 4025 Color Squares(BFS)

    题目链接:UVALive 4025 Color Squares 按题意要求放带有颜色的块,求达到w分的最少步数. //yy:哇,看别人存下整个棋盘的状态来做,我什么都不想说了,不知道下午自己写了些什么 ...

  3. UVALive 5066 Fire Drill --BFS+DP

    题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...

  4. Robots on a grid(DP+bfs())

    链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...

  5. What a Ridiculous Election UVALive - 7672 (BFS)

    题目链接: E - What a Ridiculous Election  UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...

  6. UVALive 6665 Dragon’s Cruller --BFS,类八数码问题

    题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...

  7. UVALive 7297 bfs

    题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...

  8. UVALive 7297 Hounded by Indecision BFS

    题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...

  9. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

随机推荐

  1. HasMap

    您还未登录 ! 登录 注册 论坛首页 → Java企业应用论坛 → 深入理解HashMap 全部 Hibernate Spring Struts iBATIS 企业应用 Lucene SOA Java ...

  2. xcode中控件共有属性

    Text,title 控件上的文字 backgroung 背景图片或颜色 alignment 布局(居中,..) mode显示模式(缩放,拉伸..) drawing 隐藏控件的可见性 alpha 透明 ...

  3. Boost.Python:使用继承

    An example #include <boost/python.hpp> #include <memory> #include <iostream> using ...

  4. VirtualBox安装linux增强工具报错

    错误提示: Building the OpenGL support module                         [FAILED] 解决办法 cd /media/VBOXADDITIO ...

  5. 基于TcpDump和pcap文件分析的Android平台网络抓包程序设计与实现【随便】

    一.考虑使用Tcpdump,将抓到的包保存到cap文件中,然后手动分析.参考资料:1. http://www.cnblogs.com/tt-0411/archive/2012/09/23/269936 ...

  6. Android周笔记(9.8-14)(持续更新)

    本笔记记录一周内的小知识点和一些心学习的Demo. 1.PopupWindow: new 一个activity_pop_window:id为popwindow的Button,id为hello123的T ...

  7. ContentProvider类的解析

    一.ContentProvider类 1.作用:专门用于不同应用之间进行数据共享的方式. 二.实现方法 1.创建ContenteProvider类 步骤一:继承ContentProvider接口,重写 ...

  8. NetworkManager配置网络——Red Hat 7 && CGSL V5

      NetworkManager服务管理网络方便在哪? 很重要的一点是:一个设备可以对应多个配置文件,但是同一时间只能有一个配置文件生效,这对于频率切换网络环境是非常方便的,不用再跑那个目录下去改配置 ...

  9. matlab绘制函数

    >> x1=linspace(,*pi,); x2=linspace(,*pi,); x3=linspace(,*pi,); y1=sin(x1); y2=+sin(x2); y3=+si ...

  10. 转:JavaScript函数式编程(三)

    转:JavaScript函数式编程(三) 作者: Stark伟 这是完结篇了. 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会 ...