2016SDAU程式設計練習二1018
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.<br><br>The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.<br>
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:<br><br>'X': a block of wall, which the doggie cannot enter; <br>'S': the start point of the doggie; <br>'D': the Door; or<br>'.': an empty block.<br><br>The input is terminated with three 0's. This test case is not to be processed.<br>
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.<br>
Sample Input
4 4 5<br>S.X.<br>..X.<br>..XD<br>....<br>3 4 5<br>S.X.<br>..X.<br>...D<br>0 0 0<br>
Sample Output
NO<br>YES<br>
Author
ZHANG, Zheng
Source
ZJCPC2004
題意:在座標系內給起點和終點,問是否能在t時刻到達
思路:DFS,借鑑了別人的,在網上又搜還要剪枝什麼的
感想:感覺搜尋題都長得差不多,走來走去走來走去。。。
AC程式碼:
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,t,ex,ey,tt,falg;
char mapp[10][10];
int tox[5]={0,0,1,-1};
int toy[5]={1,-1,0,0};
void dfs(int x,int y,int num)
{
if(x<0||y<0||x>=n||y>=m)
return;
if(mapp[x][y]=='X')
return ;
if(x==ex&&y==ey&&num==t)
falg=1;
if(falg)
{
tt=num;
return ;
}
int tem=t-fabs(x-ex)-fabs(y-ey)-num;
if(tem<0||((tem%2)==1))
return ;
for(int i=0;i<4;i++)
{
mapp[x][y]='X';
dfs(x+tox[i],y+toy[i],num+1);
mapp[x][y]='.';
}
}
int main()
{
// freopen("r.txt","r",stdin);
int i,j,sx,sy;
while(cin>>n>>m>>t)
{
if((t+n+m)==0) break;
int wall=0;
for(i=0;i<n;i++)
{
scanf("%s",mapp[i]);
for(j=0;j<m;j++)
{
if(mapp[i][j]=='S')
{
sx=i;
sy=j;
}
if(mapp[i][j]=='D')
{
ex=i;
ey=j;
}
if(mapp[i][j]=='X')
wall++;
}
}
if((n*m-wall-1)<t)
{
cout<<"NO"<<endl;
continue;
}
falg=0;
dfs(sx,sy,0);
if(falg)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
相關推薦
2016SDAU程式設計練習二1018
Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked
2016SDAU程式設計練習二1002
Strange fuction Problem Description Now, here is a fuction:<br> F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <
2016SDAU程式設計練習二1024
Sequence one Problem Description Search is important in the acm algorithm. When you want to solve a problem by using the search method,
2016SDAU程式設計練習二1015
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the short
2016SDAU程式設計練習二1008
猜數字 A有1數m,B來猜.B每猜一次,A就說太大t太小或;對了問B猜n次可以猜到的最大數。 Input 第1行是整數T,表示有T組資料,下面有T行 <br>每行一個整數n (1 ≤ n ≤ 30) <br> Output 猜n次可以猜到
2016SDAU程式設計練習二1009
連連看 Problem Description “連連看”相信很多人都玩過。沒玩過也沒關係,下面我給大家介紹一下游戲規則:在一個棋盤中,放了很多的棋子。如果某兩個相同的棋子,可以通過一條線連起來(這條線不能經過其它棋子),而且線的轉折次數不超過兩次,那麼這兩個棋子就可以
2016SDAU程式設計練習二1025
Sequence two Problem Description Search is important in the acm algorithm. When you want to solve a problem by using the search method,
2016SDAU程式設計練習二1013
A strange lift Problem Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0
2016SDAU程式設計練習三1007
Problem G Problem Description都說天上不會掉餡餅,但有一天gameboy正走在回家的小徑上,忽然天上掉下大把大把的餡餅。說來gameboy的人品實在是太好了,這餡餅別處都不掉,就掉落在他身旁的10米範圍內。餡餅如果掉在了地上當然就不能吃了,所以
2016SDAU程式設計練習三1010
Problem J Problem Description 有一樓梯共M級,剛開始時你在第一級,若每次只能跨上一級或二級,要走上第M級,共有多少種走法? Input 輸入資料首先包含一個整數N,表示測試例項的個數,然後是N行資料,每行包含一個整數M(1<=M&l
2016SDAU程式設計練習三1012
Problem L Problem Description 在2×n的一個長方形方格中,用一個1× 2的骨牌鋪滿方格,輸入n ,輸出鋪放方案的總數.<br>例如n=3時,為2× 3方格,骨牌的鋪放方案有三種,如下圖:<br><img src=
2016SDAU程式設計練習三1001
Problem A Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For
2016SDAU程式設計練習三1014
Problem N Problem Description 我們看到過很多直線分割平面的題目,今天的這個題目稍微有些變化,我們要求的是n條折線分割平面的最大數目。比如,一條折線可以將平面分成兩部分,兩條折線最多可以將平面分成7部分,具體如下所示。<br><
2016SDAU程式設計練習三1017
Problem Q Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to col
C++Primer Plus筆記——第十二章 類和動態記憶體分配課後程式設計練習答案
目錄 課後習題 習題1 習題2 習題3 習題4 習題5&6 課後習題 習題1 #include <iostream> #include <cstring> using namespace std; class Cow {
函式程式設計實驗二:遞迴練習
module HW where {- 1. 定義求兩個非負整數最大公因子的函式: mygcd ::Integer ->Integer ->Integer -} mygcd ::Integer ->Integer ->Integer my
C primer plus 第六版 第六章 第十二題 程式設計練習答案
#include<stdio.h> #define l 1.0 int main(void) { int i = 0; // Create for loop. i
C primer plus 第六版 第十章 第十二題 程式設計練習答案
Github地址: φ(>ω<*)這裡這裡。 /* 本程式應 習題-12 建立。 題目要求: 重寫程式清單 10.7 的 rain.c 程式,把 main() 中的主要任務都改成用函式來完成。 */ #inc
Flink程式設計練習(二)
Map 班級學生成績的隨機生成 輸入:本班同學的學號 輸出:<學號,成績> 資料準備 首先需要一個stuID.csv檔案,每一列為一個學號: 然後將檔案放入HDFS中: hdfs d
遊戲程式設計模式(Game Programming Patterns)的C#&Unity練習二:觀察者模式
觀察者模式 核心描述:“在物件間定義一種 一對多的依賴關係,以便當某個物件的情況發生改變時,與它存在依賴關係的所有物件都能收到通知,並自動進行更新。” 知識點: 1.複習了“繼承”和“多型”,包括讓子類以介面的身份進行活動。子類可以直接使用父類的方法。