最短路

吐槽一下。。。最先开始写了个地图哈希,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的更多相关文章

随机推荐

  1. 基于证书的MS SQL2005数据库镜像搭建

    一.准备工作: 3台服务器同版本,硬盘分区大小相同,安装相同版本数据库软件. host中分别标注3台服务器IP和主机名称. 主体服务器上创建数据库,并进行完整备份数据库和数据库事务. 拷贝备份文件给镜 ...

  2. Tcl之looping

    1 While loop while test body The while command evaluates test as an expression. If test is true, the ...

  3. CAD在网页中得到批注信息

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. Miller Rabbin素数测试

    步骤 ①先写快速幂取模函数 ②MR算法开始 (1)传入两个参数一个是底数一个是n也就是幂数,如果n是一个合数那么可以判定,这个数一定不是素数 (2)然后开始寻找一个奇数的n去计算,如果最后满足a^d% ...

  5. Exception总结

    NO.1 java.lang.NullPointerException 程序遇上了空指针 NO.2 java.lang.ClassNotFoundException 指定的类不存在 NO.3 java ...

  6. Keil-MDK编译完成后代码大小

    Code 代表执行的代码,程序中所有的函数都位于此处. RO-data 代表只读数据,程序中所定义的全局常量数据和字符串都位于此处. RW-data 代表已初始化的读写数据,程序中定义并且初始化的全局 ...

  7. 敏捷开发系列学习总结(2)——Bug修改流程

    原则,力求各司其职,简单明了. 1. 测试人员提交bug ⑴ 标题: [ 模块名称 ] 问题描述 ⑵ 内容: 问题重现步骤的描述,最好贴上图片. 因为一图胜万言. ⑶ 指定责任人: 根据bug指定责任 ...

  8. JavaSE 学习笔记之多态(七)

    多 态:函数本身就具备多态性,某一种事物有不同的具体的体现. 体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat(); 多态的好处:提高了程序的扩展性. 多态的 ...

  9. hdu 4171 最短路

    #include<stdio.h> #include<string.h> #include<queue> #include<iostream> usin ...

  10. Linux I/O scheduler for solid-state drives

    An I/O scheduler and a method for scheduling I/O requests to a solid-state drive (SSD) is disclosed. ...