UVA 10452 Marcus, help!( DFS ),10452dfs
Problem I
Marcus, help!
Input: standard input
Output: standard output
Time Limit: 2 Seconds
"First, the breath of God.
Only the penitent man will pass.
Second, the Word of God,
Only in the footsteps of God will he proceed.
Third, the Path of God,
Only in the leap from the lion's head will he prove his worth."
(taken from the movie "Indiana Jones and the Last Crusade", 1989)
To get to the grail, Indiana Jones needs to pass three challenges. He successfully masters the first one and steps up to the second. A cobblestone path lies before him, each cobble is engraved with a letter. This is the second challenge, the Word of God, the Name of God. Only the cobbles having one of the letters "IEHOVA" engraved can be stepped on safely, the ones having a different letter will break apart and leave a hole.
Unfortunately, he stumbles and falls into the dust, and the dust comes into his eyes, making it impossible for him to see. So he calls for Marcus Brody and asks Marcus to tell him in which
direction to go to safely reach the other side of the cobblestone path. Because of the dust in his eyes, Indy only can step "forth" to the stone right in front of him or do a side-step to the stone on the "left" or the "right" side of him. These ("forth", "left", "right") are also the commands Marcus gives to him.
InputThe first line of the input contains a single number indicating the number of test cases that follow.Each test case starts with a line containing two numbers m and n (2 <= m, n <= 8), the length m and the width n of the cobblestone path. Then follow m lines, each containing n characters ('A' to 'Z', '@', '#'), the engravement of the respective cobblestone. Indy's starting position is marked with the '@' character in the last line, the destination with the character '#' in the first line of the cobblestone path. Each of the letters in "IEHOVA" and the characters '@' and '#' appear exactly once in each test case. There will always be exactly one path from Indy's starting position via the stones with the letters "IEHOVA" engraved on (in that order) to the destination. There will be no other way to safely reach the destination. Output
For each test case, output a line with the commands Marcus gives to Indy so that Indy safely reaches the other side. Separate two commands by one space character.
Sample Input26 5PST#TBTJASTYCVMYEHOFXIBKUN@RJB5 4JA#XJVBNXOHDDQEMT@IY Sample Outputforth forth right right forth forth forthright forth forth left forth forth right
Problem-setter: Juergen Werner, Albert Einstein University of ULM, Germany
題目大意:
給你一個矩陣,只能按照IEHOVA的順序走,輸出軌跡的方向。@為起始點,#為終止點。
解題思路:
DFS,注意是從下往上走,i是減一的。
代碼:
#include<iostream>#include<cstdio>#include<string>using namespace std;const int maxN=10;const char letter[8]={'@','I','E','H','O','V','A','#'};const string str0[3]={"left","forth","right"};int dirX[3]={-1,0,1},n,m;int dirY[3]={0,-1,0};//i是減不是加。string mymap[maxN];bool cmp(int di,int dj){ if(di>=0&&di<n&&dj>=0&&dj<m) return true; return false;}void dfs(int i,int j,int depth){ for(int k=0;k<3;k++){ int di=i+dirY[k],dj=j+dirX[k]; if(cmp(di,dj)&&mymap[di][dj]==letter[depth]){ cout<<str0[k]; if(depth<=6) cout<<" "; dfs(di,dj,depth+1); } }}int main(){ int t,pi,pj; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); for(int i=0;i<n;i++){ cin>>mymap[i]; for(int j=0;j<m;j++){ if(mymap[i][j]==letter[0]) pi=i,pj=j; } } dfs(pi,pj,1); cout<<endl; } return 0;}#include<iostream>#include<cstdio>#include<string>using namespace std;const int maxN=10;const char letter[8]={'@','I','E','H','O','V','A','#'};const string str0[3]={"left","forth","right"};int dirX[3]={-1,0,1},n,m;int dirY[3]={0,-1,0};//i是減不是加。string mymap[maxN];bool cmp(int di,int dj){ if(di>=0&&di<n&&dj>=0&&dj<m) return true; return false;}void dfs(int i,int j,int depth){ for(int k=0;k<3;k++){ int di=i+dirY[k],dj=j+dirX[k]; if(cmp(di,dj)&&mymap[di][dj]==letter[depth]){ cout<<str0[k]; if(depth<=6) cout<<" "; dfs(di,dj,depth+1); } }}int main(){ int t,pi,pj; scanf("%d",&t); while(t--){ scanf("%d %d",&n,&m); for(int i=0;i<n;i++){ cin>>mymap[i]; for(int j=0;j<m;j++){ if(mymap[i][j]==letter[0]) pi=i,pj=j; } } dfs(pi,pj,1); cout<<endl; } return 0;}
UVa 10599 Robots(II) -- 劉汝佳書的Dp一章 -- 標程解釋 --
這個程式的最大特點是不建鄰接矩陣,而把各個目標點存進數組。
為什麼一定要求LIS呢。這樣似乎路線不明顯,求LIS可以用棧實現nlogn。這題記憶化搜尋就可以了,遍曆序號大的點,加上是否在右方下方的判斷。
UVa 548 - Tree 我怎錯了?
AC 代碼,題目中說,給你一棵二叉樹的中序遍曆和後序遍曆
這裡結點是在1到10000的範圍的,最多是10000個結點,不過這裡結點不是連續的不是說有N個點就是1到N編號
比如
1 7 8
1 8 7這種資料也會有的
下面是我的AC代碼
#include<cstdlib>
#include<stdio.h>
#include<string.h>
const int MAX=10005;
int in[MAX];
int post[MAX];
int son[MAX][2];
char s[1000000];
int p[MAX];
int ind,dis[MAX];
int deal(int x[],char s[])
{
int n=0,i;
for(i=0;s[i];i++)
{
if(s[i]>='0'&&s[i]<='9')
{
x[n]=0;
while(s[i]&&s[i]>='0'&&s[i]<='9')
{
x[n]=x[n]*10+s[i]-'0';
i++;
}
i--;
n++;
}
}
return n;
}
int DFS(int low,int high)
{
if(low>=high)return -1;
if(ind==-1)return -1;
int r=post[ind];
int rp=p[r];
if(rp>=low&&rp<high)
{
ind--;
son[r][1]=DFS(rp+1,high);
son[r][0]=DFS(low,rp);
return r;
}
else return -1;
}
void getDis(int r)
{
int t;
if(son[r][0]!=-1)
{
t=son[r][0];
dis[t]=dis[r]+t;
getDis(t);
}
if(son[r][1]!=-1)
{
t=son[r][1];
dis[t]=dis[r]+t;
getDis(t);
}
}
void inorder(int r)
{
if(son[r][0]!=-1)inorder(son[r][0]);
printf("%d ",r);
if(son[r][1]!=-1)inorder(son[r][1]);
}
void postorder(int r)
{
if(son[r][0]!=-1)postorder(son[r][0]);
if(son[r][1]!=-1)postorder(son[r][1]);
printf("%d ",r);
}
int main()
{
int n,i,k;
while(gets(s))
{
n=deal(in,s);
gets(s);
n=deal(post,s);
if(n==1)
{
printf("%d\n",in[0]);
continue;
}
memset(son,0,sizeof(son));
for(i=0;i<n;i++)p[in[i]]=i;
ind=n-2;
......餘下全文>>