zoj3478
最短路
吐槽一下。。。最先开始写了个地图哈希,6kb,然后不是正解,又写了个spfa,4kb,还是不对,无奈抄标程,结果把spfa改成dijiestra就对了。。。
由于只有两个变量,所以我们设一个四维状态,d[x0][y0][x1][y1],然后就可以跑最短路了。如果是多维的话就得用哈希了。
但是那个终点的位置不固定,而且得做两次,因为控制的价钱不一样,所以两次取最小值。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {, , -, }, dy[] = {-, , , };
struct position {
int x0, y0, x1, y1;
position(int x0 = , int y0 = , int x1 = , int y1 = ) : x0(x0), y0(y0), x1(x1), y1(y1) {}
bool friend operator < (position A, position B)
{
return A.x0 < B.x0;
}
};
int T, sx0, sy0, sx1, sy1;
queue<position> q;
set<position> inq;
int d[][][][];
char Map[][];
void spfa()
{
memset(d, 0x3f3f, sizeof(d));
q.push(position(sx0, sy0, sx1, sy1));
d[sx0][sy0][sx1][sy1] = ;
while(!q.empty())
{
position x = q.front();
q.pop();
inq.erase(x);
// if(x.x0 < 1 || x.x1 < 1 || x.y0 < 1 || x.y1 < 1 || x.x0 > 10 || x.x1 > 10 || x.y0 > 15 || x.y1 > 15 || (Map[x.x0][x.y0] == 'O' && Map[x.x1][x.y1] == 'O')) continue;
if(Map[x.x0][x.y0] == 'O' && Map[x.x1][x.y1] == 'O') continue;
for(int i = ; i < ; ++i)
{
position t = x;
int cost = d[t.x0][t.y0][t.x1][t.y1];
if(Map[t.x0][t.y0] != 'O')
{
t.x0 += dx[i];
t.y0 += dy[i];
cost += ;
if(Map[t.x0][t.y0] == 'X')
{
t.x0 = x.x0;
t.y0 = x.y0;
}
}
if(Map[t.x1][t.y1] != 'O')
{
t.x1 += dx[i];
t.y1 -= dy[i];
cost += ;
if(Map[t.x1][t.y1] == 'X')
{
t.x1 = x.x1;
t.y1 = x.y1;
}
}
if(d[t.x0][t.y0][t.x1][t.y1] > cost)
{
d[t.x0][t.y0][t.x1][t.y1] = cost;
if(inq.find(position(t.x0, t.y0, t.x1, t.y1)) == inq.end())
{
inq.insert(position(t.x0, t.y0, t.x1, t.y1));
q.push(position(t.x0, t.y0, t.x1, t.y1));
}
}
}
position t = x;
int cost = d[t.x0][t.y0][t.x1][t.y1];
if(abs(x.x0 - x.x1) + abs(x.y0 - x.y1) == && ((Map[x.x0][x.y0] == 'O') ^ (Map[x.x1][x.y1] == 'O')))
{
if(Map[x.x0][x.y0] == 'O')
{
t.x0 = x.x1;
t.y0 = x.y1;
}
else
{
t.x1 = x.x0;
t.y1 = x.y0;
}
cost += ;
}
if(d[t.x0][t.y0][t.x1][t.y1] > cost)
{
d[t.x0][t.y0][t.x1][t.y1] = cost;
if(inq.find(position(t.x0, t.y0, t.x1, t.y1)) == inq.end())
{
inq.insert(position(t.x0, t.y0, t.x1, t.y1));
q.push(position(t.x0, t.y0, t.x1, t.y1));
}
}
}
printf("%d\n", d[][][][] == ? - : d[][][][]);
}
int main()
{
scanf("%d", &T);
while(T--)
{
inq.clear();
for(int i = ; i <= ; ++i)
for(int j = ; j <= ; ++j)
Map[i][j] = 'X';
for(int i = ; i <= ; ++i)
{
char s[];
scanf("%s", s + );
for(int j = ; j <= ; ++j)
{
if(s[j] == 'H') s[j] = '.';
Map[i][j] = s[j];
}
}
scanf("%d%d%d%d", &sx0, &sy0, &sx1, &sy1);
spfa();
}
return ;
}
zoj3478的更多相关文章
随机推荐
- jQuery——this
js注册事件this代表的dom对象 jQuery注册事件this代表的也是dom对象,所以需要$(this)转成jQuery对象
- Apache服务器防范DoS
Apache服务器对拒绝服务攻击的防范主要通过软件Apache DoS Evasive Maneuvers Module 来实现.它是一款mod_access的替代软件,可以对抗DoS攻击.该软件可 ...
- centOS7卸载google-chrome
参考: https://www.jianshu.com/p/39d0b8f578d9
- Java中面向对象三大特性之——多态
多态的概述: 多态是继封装.继承之后,面向对象的第三大特性. 生活中,比如跑的动作,小猫.小狗和大象,跑起来是不一样的.再比如飞的动作,昆虫.鸟类和飞机,飞起来也是不一样的.可见,同一行为,通过不同 ...
- Windows如何正确的修改administrator用户名
无论是服务器还是电脑.修改默认的administrator的用户总是好的.话不多少.直接上图 1.win+r 输入:gpedit.msc 2.按照我下图的圈圈找到重命名系统管理员账户并自定义 3.重 ...
- mha0.56版本安装使用排错
1.master_check_ssh --conf=/etc/app1.conf 这个检查就报错的我觉得百分之九十都是ssh之间连接问题.务必要保证各节点之间都可以免秘钥访问! 2.mas ...
- PHP控制反转(IOC)和依赖注入(DI
<?php class A { public $b; public $c; public function A() { //TODO } public function Method() { $ ...
- PHP中的几个随机数生成函数
PHP中的几个随机数生成函数 rand() 基于 libc 的随机种子发生器 mt_rand() 基于 Mersenne Twister 算法返回随机整数.它可以产生随机数值的平均速度比 libc 提 ...
- Thesis Viva checklist
This list gives you suggestions helpful in preparing to defend your thesis: I know my thesis thoroug ...
- 53. Maximum Subarray(动态规划)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...