uvalive 6888 Ricochet Robots bfs
给一个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的更多相关文章
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- UVALive 4025 Color Squares(BFS)
题目链接:UVALive 4025 Color Squares 按题意要求放带有颜色的块,求达到w分的最少步数. //yy:哇,看别人存下整个棋盘的状态来做,我什么都不想说了,不知道下午自己写了些什么 ...
- UVALive 5066 Fire Drill --BFS+DP
题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...
- 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 ...
- What a Ridiculous Election UVALive - 7672 (BFS)
题目链接: E - What a Ridiculous Election UVALive - 7672 题目大意: 12345 可以经过若干次操作转换为其它五位数. 操作分三种,分别为: 操作1:交 ...
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
题意大概就是八数码问题,只不过把空格的移动方式改变了:空格能够向前或向后移动一格或三格(循环的). 分析:其实跟八数码问题差不多,用康托展开记录状态,bfs即可. 代码: #include <i ...
- UVALive 7297 bfs
题意 一个小偷偷到了项链 他想知道自己是否可以逃出去 地图中有一个小偷 一个警察 警察有一条狗 一开始 小偷和警察的移动速度都是1 当警察走到小偷经过过的地方时 警察会有一条狗嗅到小偷的气味并且以2的 ...
- UVALive 7297 Hounded by Indecision BFS
题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
随机推荐
- android开发SD卡工具类(一)
SD卡工具类整理: package com.gzcivil.utils; import java.io.File; import java.io.FileInputStream; import jav ...
- ARP网关占用
30网段已经发生了2次ARP了 排查方法:我直去核心交换机直连镜像口,用wireshark抓包,过滤出ARP的包 发现的确有ARP的攻击,因为没有统计公司电脑和无线路由的MAC地址,所以只能一个个把无 ...
- 回溯算法————n皇后、素数串
回溯就是算法是搜索算法中一种控制策略,是一个逐个试探的过程.在试探的过程中,如果遇到错误的选择,就会回到上一步继续选择下一种走法,一步一步的进行直到找到解或者证明无解为止. 如下是一个经典回溯问题n皇 ...
- Android 判断听云是否嵌入正确
编译打包成apk之后,将apk在手机上进行安装,连接数据线,打开命令行,输入以下命令: adb logcat -v time -s NBSAgent:V 之后运行嵌入听云代码的app,进行有效的网络访 ...
- 数组转DataTable
using System; using System.Data; namespace ArrayToDataTable { class ArrayToDataTable { /// <summa ...
- Node.js内置的工具和第三方模块来进行单步调试
1.命令行调试: Node.js调试命令: run 执行脚本,在第一行暂停 restart 重新执行脚本 cont,c 继续执行,知道遇到下一个断点 next,n 单步执行 step,s 单步执行,并 ...
- bugfree搭建
- python 网络编程第四版
使用SocketServer 模块来完成服务端编程 1.服务端代码如下: #!/usr/bin/python #!coding:utf-8 import SocketServer as sockets ...
- W5300E01-ARM 交叉编译器(Cross Compiler)用户手册
W5300E01-ARM是基于W5300的ARM功能测试评估板: 1 简介 当用户的开发环境与目标系统不同时就会用到交叉编译器. 例如,当开发基于ARM的嵌入式系统时,用户就需要在电脑上写出 ...
- POJ 3111 K Best(二分答案)
[题目链接] http://poj.org/problem?id=3111 [题目大意] 选取k个物品,最大化sum(ai)/sum(bi) [题解] 如果答案是x,那么有sigma(a)>=s ...