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的更多相关文章
随机推荐
- CSS——text-indent
在h1标签里套入a标签并写上文字,有利于seo,但是文字如何隐藏呢?一般都是a标签变成内联块并首行缩进为负值. <!DOCTYPE html> <html lang="en ...
- 遍历select搜索结果,只取数字标key值,防止重复
//遍历select搜索结果,只取数字标key值,防止重复 foreach ($row as $key => $value) { if (is_int($key)) { echo $value; ...
- http通信流程
Host https://www.charlesproxy.com Path / Notes SSL Proxying not enabled for this host. Enable in the ...
- 不用float也可以让div横向显示
display: inline-block; vertical-align: top; 就这两个属性,给div设置上,div就不会换行显示啦,而且还不影响横向的其他元素的显示.
- CDR如何使用钢笔工具进行完美抠图?【6·18特惠倒计时!】
不要以为抠图只能在图像处理软件中实现,矢量图形绘制软件CorelDRAW一样可以,而且方法很多,文章介绍使用CDR钢笔工具抠图的方法. 提示说明: 首先说明一下,CDR中的钢笔工具和其他平面设计软件中 ...
- css知识框架
- VM虚拟机NAT链接外网
1.vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=localhost.localdomainGATEWAY=192.168.110.2 2.vi /e ...
- matlab数值数据的表示方法,输出数据以及相关函数
数据类型的分类: 1.整型 无符号整型和带符号整形 带符号整形的最大值是127 >>x=int8(129) 输出结果是x=127 >>x=unit8(129) 输出结果是x=1 ...
- How To: Multipath Linux x86-64 Release 6.4
[root@node01 ~]# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0 ...
- H5 应用程序缓存(离线缓存)
离线缓存这个功能的实现有以下步骤: 1,以nginx做web服务器为例,在mime.types文件中添加一行:text/cache-manifest manifest,作用是为了让服务器识别该 ...