[Gym-100625J] 搜索
题目链接:https://cn.vjudge.net/problem/Gym-100625J
具体思路:首先,具体思路是两个人一起走到一个点,然后两个人按照同样的道路走出去,听了别人的思路,还有一种特殊情况需要考虑,就是中间一堵墙,两个人都直接在边界上,这个时候可以新加一个点,然后两个人到这个点的开门数都是0,然后三个搜索,新加的点到图里面所有能走的点的最小路径,两个人分别到达图内的点和新加的点的最小开门数,然后枚举每一个点,找最小就可以了。
AC代码:
#include<iostream>
#include<stack>
#include<iomanip>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<map>
#include<string>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
# define inf 100000
# define ll long long
# define maxn 100+100
int dis[maxn][maxn];// 计算从外面的点到里面每个点的最小开门数
int num1[maxn][maxn];// 计算第一个人到达某一个点的最小开门数
int num2[maxn][maxn];//计算第二个人到达某一个点的最小开门数
int vis[maxn][maxn];//dfs的过程中标记
char Map[maxn][maxn];
int n,m,k;
int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
bool judge(int t1,int t2)
{
if(t1>=0&&t2>=0&&t1<=n+1&&t2<=m+1)return true;
return false;
}
struct node
{
int x,y,num;
node(int xx,int yy,int zz)
{
x=xx;
y=yy;
num=zz;
}
friend bool operator < (node t1,node t2)
{
return t2.num<t1.num;//把想要的放在后面,符号方向不变
}
};
void bfs1(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
dis[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
dis[t1][t2]=dis[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
dis[t1][t2]=dis[temp.x][temp.y];
}
}
}
}
}
void bfs2(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
num1[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
num1[t1][t2]=num1[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
num1[t1][t2]=num1[temp.x][temp.y];
}
}
}
}
}
void bfs3(int x,int y)
{
priority_queue<node>q;
q.push(node(x,y,0));
vis[x][y]=1;
num2[x][y]=0;
while(!q.empty())
{
node temp=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int t1=temp.x+f[0][i];
int t2=temp.y+f[1][i];
if(judge(t1,t2)&&vis[t1][t2]==0&&Map[t1][t2]!='*')
{
vis[t1][t2]=1;
if(Map[t1][t2]=='#')
{
q.push(node(t1,t2,temp.num+1));
num2[t1][t2]=num2[temp.x][temp.y]+1;
}
else
{
q.push(node(t1,t2,temp.num));
num2[t1][t2]=num2[temp.x][temp.y];
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
int x1,y1,x2,y2;
while(T--)
{
int flag=1;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",Map[i]+1);
}
for(int i=0; i<=m+1; i++)
{
Map[0][i]='.';
Map[n+1][i]='.';
}
for(int i=0; i<=n+1; i++)
{
Map[i][0]='.';
Map[i][m+1]='.';
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(Map[i][j]!='$')continue;
if(flag==1)
{
x1=i;
y1=j;
flag=0;
}
else if(flag==0)
{
x2=i;
y2=j;
break;
}
}
}
for(int i=0; i<=n+1; i++)
for(int j=0; j<m+1; j++)
{
dis[i][j]=inf;
num1[i][j]=inf;
num2[i][j]=inf;
}
memset(vis,0,sizeof(vis));
bfs1(0,0);
memset(vis,0,sizeof(vis));
bfs2(x1,y1);
memset(vis,0,sizeof(vis));
bfs3(x2,y2);
int minn=inf;
minn=min(minn,dis[0][0]+num1[0][0]+num2[0][0]);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(Map[i][j]=='#')
minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]-2);
else
minn=min(minn,dis[i][j]+num1[i][j]+num2[i][j]);
}
}
printf("%d\n",minn);
}
return 0;
}
/*
5 9
****#****
*..#.#..*
****.****
*$#.#.#$*
*********
5 11
*#*********
*$*...*...*
*$*.*.*.*.*
*...*...*.*
*********.*
9 9
*#**#**#*
*#**#**#*
*#**#**#*
*#**.**#*
*#*#.#*#*
*$##*##$*
*.#.#.#.*
*********
*/
[Gym-100625J] 搜索的更多相关文章
- Gym - 100625J Jailbreak 最短路+搜索
http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...
- 暑假集训-WHUST 2015 Summer Contest #0.2
ID Origin Title 10 / 55 Problem A Gym 100625A Administrative Difficulties 4 / 6 Problem B Gym 1006 ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- GYM 100608G 记忆化搜索+概率 2014-2015 Winter Petrozavodsk Camp, Andrew Stankevich Contest 47 (ASC 47)
https://codeforces.com/gym/100608 题意: 两个人玩游戏,每个人有一个长为d的b进制数字,两个人轮流摇一个$[0,b-1]$的骰子,并将选出的数字填入自己的d个空位之中 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- 【每日dp】 Gym - 101889E Enigma 数位dp 记忆化搜索
题意:给你一个长度为1000的串以及一个数n 让你将串中的‘?’填上数字 使得该串是n的倍数而且最小(没有前导零) 题解:dp,令dp[len][mod]为是否出现过 填到第len位,余数为mod 的 ...
随机推荐
- 最简单的Linux下apache+mysql+php安装
转载:http://www.jb51.net/article/29843.htm ubuntu下需要先更新系统后 流程笔记: 1.打开终端,输入“sudo apt-get install apache ...
- PHP内置标准类
PHP内置标准类 php语言内部,有“很多现成的类”,其中有一个,被称为“内置标准类”. 这个类“内部”可以认为什么都没有,类似这样: class stdclass{ } 其作用,可以用于存储一些临 ...
- 第113天:Ajax跨域请求解决方法
一.原生JS实现ajax 第一步获得XMLHttpRequest对象 第二步:设置状态监听函数 第三步:open一个连接,true是异步请求 第四部:send一个请求,可以发送一个对象和字符串,不需要 ...
- 51nod 1574 排列转换(贪心+鸽巢原理)
题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...
- 转发---[沧海拾遗]java并发之CountDownLatch、Semaphore和CyclicBarrier
JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch CountDownLatch是一个计 ...
- BZOJ4719 NOIP2016天天爱跑步(线段树合并)
线段树合并的话这个noip最难题就是个裸题了. 注意merge最后return x,以及如果需要区间查询的话这里还需要up,无数次死于这里. #include<iostream> #inc ...
- lxm --- ans lb config
lxm --- ans lb config #ANS2.2 Build 160.006 # Last modified by `save config`, Fri Oct 12 17:15:42 20 ...
- NOIP2016愤怒的小鸟 题解报告 【状压DP】
题目什么大家都清楚 题解 我们知道,三点确定一条抛物线,现在这条抛物线过原点,所以任意两只猪确定一条抛物线.通过运算的出对于两头猪(x1,y1),(x2,y2),他们所在抛物线a=(y1*x2-y2* ...
- Sqoop数据迁移工具
一.概述 sqoop 是 apache 旗下一款“ Hadoop 和关系数据库服务器之间传送数据”的工具. 导入数据: MySQL, Oracle 导入数据到 Hadoop 的 HDFS. HIVE. ...
- 【loj2639】[Tjoi2017]不勤劳的图书管理员
#2639. 「TJOI2017」不勤劳的图书管理员 题目描述 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产 ...