Algorithm


problem link- https://www.spoj.com/problems/AMR11G/

AMR11G - Here Be Dragons

no tags 

 

The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the other end. The corridor is N segments long. The ith segment is either empty or has a dragon. Harry cannot pass the dragon and will have no option but to retreat if he encounters one. Is it possible for him to reach the exit starting from the entrance?

Input (STDIN):
The first line contains the number of test cases T.
Each of the next T lines contains a string describing the corridor. The ith character is either a '.' if the segment is empty, or a 'D' if the segment contains a dragon.
Output (STDOUT):
Output T lines, each containing either the string "Possible" if you can reach the exit, and "You shall not pass!" if it is not possible to reach the exit.
Constraints:
1 <= T <= 50
1 <= N <= 50
Time Limit: 1 s
Memory Limit: 32 MB
Sample Input:
3
..
..D.
D..D
Sample Output:
Possible
You shall not pass!
You shall not pass!

 

The Triwizard Tournament's third task is to negotiate a corridor of many segments, and reach the other end. The corridor is N segments long. The ith segment is either empty or has a dragon. Harry cannot pass the dragon and will have no option but to retreat if he encounters one. Is it possible for him to reach the exit starting from the entrance?

 

Input (STDIN):

The first line contains the number of test cases T.

Each of the next T lines contains a string describing the corridor. The ith character is either a '.' if the segment is empty, or a 'D' if the segment contains a dragon.

 

Output (STDOUT):

Output T lines, each containing either the string "Possible" if you can reach the exit, and "You shall not pass!" if it is not possible to reach the exit.

 

Constraints:

1 <= T <= 50

1 <= N <= 50

 

Sample Input:

3

..

..D.

D..D

 

Sample Output:

Possible

You shall not pass!

You shall not pass!

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#include <list>

using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		char x[50];
		scanf("%s",x);
		bool possible=true;
		for(int i=0;i<strlen(x);i++){
			if(x[i]=='D'){
				possible=false;
				break;
			}
		}
		if(possible){
			printf("Possible\n");
		}
		else{
			printf("You shall not pass!\n");
		}
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
..
..D.
D..D

Output

x
+
cmd
Possible
You shall not pass!
You shall not pass!
Advertisements

Demonstration


SPOJ Solution-AMR11G  Here Be Dragons- Solution in C, C++, Java, Python,AMR11G,SPOJ Solution

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python