百練 4116 拯救行動(bfs)
描述
公主被惡人抓走,被關押在牢房的某個地方。牢房用N*M (N, M <= 200)的矩陣來表示。矩陣中的每項可以代表道路(@)、牆壁(#)、和守衛(x)。 英勇的騎士(r)決定孤身一人去拯救公主(a)。我們假設拯救成功的表示是“騎士到達了公主所在的位置”。由於在通往公主所在位置的道路中可能遇到守衛,騎士一旦遇到守衛,必須殺死守衛才能繼續前進。 現假設騎士可以向上、下、左、右四個方向移動,每移動一個位置需要1個單位時間,殺死一個守衛需要花費額外的1個單位時間。同時假設騎士足夠強壯,有能力殺死所有的守衛。 給定牢房矩陣,公主、騎士和守衛在矩陣中的位置,請你計算拯救行動成功需要花費最短時間。
輸入
第一行為一個整數S,表示輸入的資料的組數(多組輸入)
隨後有S組資料,每組資料按如下格式輸入
1、兩個整數代表N和M, (N, M <= 200).
2、隨後N行,每行有M個字元。”@”代表道路,”a”代表公主,”r”代表騎士,”x”代表守衛, “#”代表牆壁。
輸出
如果拯救行動成功,輸出一個整數,表示行動的最短時間。
如果不可能成功,輸出”Impossible”
樣例輸入
2
7 8
#@#####@
#@a#@@[email protected]
#@@#[email protected]@@
@@#@@#@#
#@@@##@@
@#@@@@@@
@@@@@@@@
13 40
@ [email protected]@##[email protected]#[email protected]#xxxx##@#[email protected]@@#x#@#x#@@[email protected]#@x
xx###[email protected]#@@##[email protected]@@#@[email protected]@#[email protected]@@#[email protected]#[email protected]@[email protected]
#@x#@x#x#@@##@@x#@xx#[email protected]@x##@@@#@[email protected]@[email protected]
@##[email protected]@@x#xx#@@#xxxx#@@[email protected]@#@[email protected]@@[email protected]#@#[email protected]#
@#xxxxx##@@x##[email protected]@@#[email protected]####@@@x#x##@#@
#xxx#@#x##[email protected]@#[email protected]@@[email protected]#@#[email protected]#####
#[email protected]#@[email protected]@@@##@x#xx#[email protected]#xx#@#####x#@x
xx##@#@x##x##x#@x#@a#[email protected]##@#@##[email protected]#@@[email protected]
x#x#@[email protected]#x#@##@[email protected]#[email protected]##x##xx#@#[email protected]@
#[email protected]@#@###x##[email protected]#@@#@@[email protected]@@[email protected]@@@##@@[email protected]@x
x#[email protected]###@xxx#@#x#@@###@#@##@x#@[email protected]#@@#@@
#@#[email protected]#x#x###@[email protected]@xxx####[email protected]##@x####xx#@x
#x#@x#x######@@#[email protected]#xxxx#[email protected]@@#xx#x#####@
樣例輸出
13
7
思路
這道題用普通bfs可以過樣例,但是提交過不了,這道題是看了別人程式碼做的,
有兩種方法.
一種是用priority_queue 做(暫時不太懂)
另一種方法是拆點
拆點就是對於 ‘x’ ,它的擴充套件要分兩次:
對於(‘x’,0) ,它只能擴展出 ( ‘x’ , 1 )
對於(‘x’,1) , 它可以往四個方向擴充套件.
第一種 priority_queue
/*************************************************************************
> File Name: bl4116.cpp
> Author:ukiy
> Mail:
> Created Time: 2016年12月03日 星期六 22時34分06秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<queue>
int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}};
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
#define inf 0x3f3f3f
#define ll long long
#define pi acos(-1)
int dire3[6][3]={ {0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0} };
using namespace std;
const int maxn=205;
int visit[maxn][maxn];
char a[maxn][maxn];
int n,m;
int sx,sy,dx,dy;
struct node{
int x,y,step;
bool operator < (const node& p) const {
return step>p.step;
}
node(int xx,int yy,int s):x(xx),y(yy),step(s){};
};
int bfs(int x,int y){
priority_queue<node> q;
q.push(node(x,y,0));
visit[x][y]=1;
while(!q.empty()){
int hx=q.top().x,hy=q.top().y,hs=q.top().step;q.pop();
if(hx==dx && hy==dy) return hs;
rep(i,0,3){
int tx=hx+dire[i][0];
int ty=hy+dire[i][1];
if(tx<0||tx>=n||ty<0||ty>=m||a[tx][ty]=='#'||visit[tx][ty])
continue;
if(a[tx][ty]=='x') q.push(node(tx,ty,hs+2));
else q.push(node(tx,ty,hs+1));
visit[tx][ty]=1;
}
}
return -1;
}
/*
int bfs(){
int front=0;
int rear=1;
q[front].x=sx,q[front].y=sy,q[front].step=0;
visit[sx][sy]=1;
int flag=0;
while(front < rear){
if(q[front].x==dx && q[front].y==dy) return q[front].step;
rep(i,0,3){
int px=q[front].x+dire[i][0];
int py=q[front].y+dire[i][1];
if(px<0||px>=n||py<0||py>=m||visit[px][py]||a[px][py]=='#')
continue;
else{
q[rear].x=px;
q[rear].y=py;
if(a[px][py]=='@'||a[px][py]=='a')
q[rear].step=q[front].step+1;
else if(a[px][py]=='x')
q[rear].step=q[front].step+2;
visit[px][py]=1;
rear++;
}
}
front++;
}
return -1;
}
*/
int main()
{
std::ios::sync_with_stdio(false);
#ifndef OnlineJudge
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
int t;
cin>>t;
while(t--){
cin>>n>>m;
rep(i,0,n-1){
rep(j,0,m-1){
cin>>a[i][j];
if(a[i][j]=='r') sx=i,sy=j;
if(a[i][j]=='a') dx=i,dy=j;
}
}
memset(visit,0,sizeof(visit));
int ans=bfs(sx,sy);
if(ans==-1) printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}
第二種 拆點
/*************************************************************************
> File Name: bl4116_2.cpp
> Author:ukiy
> Mail:
> Created Time: 2016年12月04日 星期日 01時19分50秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<queue>
int dire[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
int dire2[8][2]={{-1,-1},{-1,0},{-1,1},{ 0,-1},{ 0,1},{ 1,-1},{ 1,0},{ 1,1}};
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
#define inf 0x3f3f3f
#define ll long long
#define pi acos(-1)
int dire3[6][3]={ {0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0} };
using namespace std;
#define maxn 205
struct node{
int r,c,t;
node (int r,int c,int t):r(r),c(c),t(t){}
};
char g[maxn][maxn];
int G[maxn][maxn];
int vis[maxn][maxn];
int sx,sy,dx,dy;
int n,m;
int bfs(){
int ans=inf;
queue<node> q;
q.push(node(sx,sy,0));
while(!q.empty()){
node p = q.front();
if(g[p.r][p.c]=='a'){
ans=min(ans,p.t);
q.pop();
break;
}
if(g[p.r][p.c]=='x'&&G[p.r][p.c]==1){ //第二次拆點
q.push(node(p.r,p.c,p.t+1));
G[p.r][p.c]=0;
}
else{
rep(i,0,3){
int tr=p.r+dire[i][0];
int tc=p.c+dire[i][1];
if(tr>=0&&tr<n&&tc>=0&&tc<m && G[tr][tc]){
if(g[tr][tc]!='x'){
q.push(node(tr,tc,p.t+1));
G[tr][tc]=0;
}
if(g[tr][tc]=='x'&&G[tr][tc]==2){//第一次拆點
q.push(node(tr,tc,p.t+1));
G[tr][tc]=1;
}
}
}
}
q.pop();
}
if(ans==inf)
return -1;
else
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
#ifndef OnlineJudge
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
int T;
cin>>T;
while(T--){
cin>>n>>m;
rep(i,0,n-1){
rep(j,0,m-1){
cin>>g[i][j];
if(g[i][j]=='x') G[i][j]=2;
else if(g[i][j]=='@'||g[i][j]=='a') G[i][j]=1;
else if(g[i][j]=='r') G[i][j]=0,sx=i,sy=j;
else G[i][j]=0;
}
}
//memset(G,0,sizeof(G));
int ans=bfs();
if(ans==-1) printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}
相關推薦
百練 4116 拯救行動(bfs)
描述 公主被惡人抓走,被關押在牢房的某個地方。牢房用N*M (N, M <= 200)的矩陣來表示。矩陣中的每項可以代表道路(@)、牆壁(#)、和守衛(x)。 英勇的騎士(r)決定孤身一人去拯救公主(a)。我們假設拯救成功的表示是“騎士到達了公主所在的位
百練 4116 拯救行動
總時間限制: 1000ms 記憶體限制: 65536kB 描述 公主被惡人抓走,被關押在牢房的某個地方。牢房用N*M (N, M <= 200)的矩陣來表示。矩陣中的每項可以代表道路(@)、牆壁(#)、和守衛(x)。 英
【遞迴+BFS】百練4115:鳴人和佐助
有幾點要總結的:1、做題時候上一道是dfs,結果先入為主,直接用的dfs。做是做出來了但是超時。dfs:判斷解是否存在性bfs:找到最優解,這道題是要找到時間最短的,最先遇到佐助的路徑一定是最優路徑,因此應該用bfs。同時他還增加了鑽石數的限定。2、然而這道題有煩人的查克拉問題,可能最短路會由於全是守衛,所以
百練2812:惱人的青蛙
style using gif ret 青春 stream rac ring its 傳送門:http://bailian.openjudge.cn/practice/2812/ 【題解】 垃圾題目毀我青春。 暴力枚舉兩個點,判斷是否成立。 瞎jb判一判,剪剪枝就過了。 大
百練3383:Cell Phone Network
%d ble set 實現 names sca amp register can 傳送門:http://bailian.openjudge.cn/practice/3383/ 【題解】 題目就是最小支配集。 學習了最小支配集的解法: 樹形dp(有空可以推一推) 貪心:DFS
百練2505:A multiplication game
unsigned stdio.h multi ios ica ret reg cti std 傳送門:http://bailian.openjudge.cn/practice/2505/ 【題解】 我們找找規律: 1~9顯然是Stan wins. 10~18是Ollie w
百練6255-單詞反轉-2016正式B題
tle ebo first col span top 時間 title ams 百練 / 2016計算機學科夏令營上機考試 已經結束 題目 排名 狀態 統計 提問 B:單詞翻轉 查看 提交 統計 提問 總時間限制: 1000ms 內存限制
百練3723:圍棋
pen 沒有 輸入 數據 交點 con 表白 char mage 總時間限制: 1000ms 內存限制: 65536kB描述圍棋的棋盤上有19*19條線交織成的361個交點,黑棋和白棋可以下在交點上。我們稱這些交點為“目”。一個目的上下左右四個方向,稱之為“氣”,如果一個目
百練4082:樹的鏡面映射
content front light 利用 內存 create true nod namespace 總時間限制: 1000ms 內存限制: 65536kB描述 一棵樹的鏡面映射指的是對於樹中的每個結點,都將其子結點反序。例如,對左邊的樹,鏡面映射後
百練1248:Safecracker
() als urn 字典序 %d end std puts 多個 題目鏈接:http://bailian.openjudge.cn/practice/1248/ 題目大意: 給定一個正數target(小於12000000)和一串由大寫字母組成的數組(長度為5~12),從數
百練OJ:2804詞典
temp [0 stdin 做的 std int 原來 比較 max 題目是這樣的: 2804:詞典 總時間限制: 3000ms 內存限制: 65536kB描述 你旅遊到了一個國外的城市。那裏的人們說的外國語言你不能理解。不過幸運的是,你有一本詞典可以幫助你。 輸入首先
百練OJ:2797最短前綴
包括 == art 我們 第一個字符 好用 else class 暴力 剛開始找輸入的退出方式死也沒找著,原來這並不是個問題,上網看了一下有人直接這麽寫的 while( scanf( "%s", input[ temp ++]) != EOF );輸入完成後 通過輸入一行C
百練2593:MaxSequence
target ucs dpi href sql info pbm mmc nib n剎未獵lbr牢卮排http://t.docin.com/mwbqv36209 Rq遮寐Y34倚趁嗽3NDhttp://jz.docin.com/ujzvf21356 4蹦啡7棟V灰癰
百練1088 DP+DFS
clu 偉人 學習 mov pri pre 意思 pos freopen 題幹在最後。 這道題本來是一道DP的練習題,的真不好意思的是,盡管那道更復雜的poj1661我最終做出來了,可是花太多時間了!我決定改變學習策略,多看別人的,等有底子了,再自己來! 要知道國外一位偉人
百練2811--熄燈問題
n) ++ printf cpp clas esp blank 數組 light 題目陳述 這道題乍一看不會做,但聽完老師的思路就變得很簡單: 雖然情況很多似乎列舉不完,但是我們可以只列舉第一行的所有情況,然後推出後面的所有情況,這下這道題就有了思路 我自己寫的時候沒有用到
遞歸+DFS--簡單迷宮問題--百練2802
sizeof pair printf c代碼 freopen game 一個 mem getch 題目描述 遞歸函數中為什麽最後有一個 =false? ac代碼: #include<iostream> #include<cstring> #defi
18.06.30 POJ 百練1745:Divisibility
ted 描述 來源 str %d std fir src lap 描述 Consider an arbitrary sequence of integers. One can place + or - operators between integers in the s
18.06.30 POJ 百練2192:Zipper
img string IE osi AC and origin AI XA 描述 Given three strings, you are to determine whether the third string can be formed by combining t
百練2757:最長上升子序列
復雜度 per 都在 ... sub std () wrap urn 描述 一個數的序列bi,當b1 < b2 < ... < bS的時候,我們稱這個序列是上升的。對於給定的一個序列(a1, a2, ..., aN),我們可以得到一些上升的子序列(
百練4152:最佳加法表達式(dp+高精度)
大整數 輸入 println sca ger 最小值 nbsp oid style 描述 給定n個1到9的數字,要求在數字之間擺放m個加號(加號兩邊必須有數字),使得所得到的加法表達式的值最小,並輸出該值。例如,在1234中擺放1個加號,最好的擺法就是12+34,和為36