最短路

吐槽一下。。。最先开始写了个地图哈希,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. JS——input标签注册事件

    注意:淘宝的lable是用定位制作的,事件是oninput事件 <!DOCTYPE html> <html> <head lang="en"> ...

  2. JS——i++与++i

    先赋值后自增: var i = 0; var n1 = i++; alert(i);//返回1 alert(n1);//返回0 先自增后赋值: var i = 0; var n1 = ++i; ale ...

  3. Java_Web三大框架之Hibernate配置文件(二)

    下面介绍一下编写Hibernate的配置文件,使用Hibernate操作数据库. 开始部署:下载需要的jar包               下载Hibernate           Hibernat ...

  4. (二)Python 学习第二天--爬5068动漫图库小案例

    (注:代码和网站仅仅是学习用途,非营利行为,源代码参考网上大神代码,仅仅用来学习

  5. (一)Python 学习第一天--基础知识,列表

    1. .pyc文件 .pyc文件:在python3中,当模块运行时会自动生成在_pycache_文件夹中,其中c为compiled的缩写. Python是一门现编译后解释的语言,在运行时首先寻找.py ...

  6. Docker是什么?可以用Docker做什么?

    作者:刘允鹏 链接:https://www.zhihu.com/question/28300645/answer/67707287 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  7. VMware虚拟机下Ubuntu安装VMware Tools详解

    一.安装步骤 1.开启虚拟机,运行想要安装VMware Tools的系统,运行进入系统后,点击虚拟机上方菜单栏的“虚拟机(M)”->点击“安装 VMware Tools”,图片所示是因为我已经安 ...

  8. Getmemory问题

    题目一: [cpp] view plaincopy void GetMemory( char *p ) { p = ( ); } void Test( void ) { char *str = NUL ...

  9. Dinic当前弧优化 模板及教程

    在阅读本文前,建议先自学最大流的Ek算法. 引入 Ek的核心是执行bfs,一旦找到增广路就停下来进行增广.换言之,执行一遍BFS执行一遍DFS,这使得效率大大降低.于是我们可以考虑优化. 核心思路 在 ...

  10. golang实现高阶函数之filter

    package main import "fmt" type student struct{ name string grade int8 } func filter(stu [] ...