[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 的 ...
随机推荐
- php PDO操作类
<?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...
- C语言为运算及 两个变量的赋值问题
#include <stdio.h>#define ARRAY_SIZE 10int main() { int arr[ARRAY_SIZE] = {51,116,53,120,85 ...
- PHP伪类型和伪变量
一.伪类型 PHP伪类型有三种,分别是:1,mixed混合类型.2,number数字类型.3,callback回调类型. 1,mixed混合类型: mixed说明一个参数可以接受多种不同的类型,但并不 ...
- IPv4编址及子网划分
在讨论IP编址之前,我们需要讨论一下主机与路由器连入网络的方法.一台主机通常只有一条链路链接到网络:当主机中的IP想发送一个数据报时,它就在链路上发送,主机与物理链路之间的边界叫做接口(interfa ...
- 【数据库】百万级数据库SQL优化大总结
网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 这篇文章我花费了大量的时间查找资料.修改.排版,希望大家阅读之后,感觉 ...
- 【.Net】输出的字符靠右对齐
先看下面的这组字符,如果输出来,它是无法靠右对齐: " }; foreach (string s in s1) { string s2 = s; Console.WriteLine(s2); ...
- 【Python】Python基础教程系列目录
Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. 在现在的工作及开发当中,Python的使用越来越广泛,为了方便大家的学习,Linux大学 特推出了 <Python基 ...
- Golang基础(一)
1. 变量声明与赋值 // var.go package main import "fmt" var a string var b bool var c string = &quo ...
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
- 创建Qt项目
创建Qt项目 1 创建Qt项目 2.1 使用向导创建 打开Qt Creator 界面选择 New Project或者选择菜单栏 [文件]-[新建文件或项目]菜单项 弹出New Project对 ...