Who can help me?Please give me some cases I can't handle or point out my mistake.Thanks very much.
The following is my code:
Code: Select all
Accepted!
Moderator: Board moderators
Code: Select all
Accepted!
Code: Select all
A:DBC. A B 20
#
Code: Select all
/C
Code: Select all
/D
Code: Select all
A:BCD;B:AD;D:BG;F:H;G:DEH;E:FGH;H:EG;C:AD. A C 3
A:BCD;B:ACD;C:ABD;D:ABCG;G:DEFH;E:GFH;F:GEH;H:EFG. A C 1
A:BCD;B:ACD;C:ABD;D:ABCG;G:DEFH;E:GFH;F:GEH;H:EFG. A C 6
A:BCD;B:ACD;C:ABD;D:ABCG;G:DEFH;E:GFH;F:GEH;H:EFG. A C 7
A:B;B:A. B A 3
A:B;B:C;C:A. B A 6
C:A;A:B. A C 999999999
A:BCD;B:AD;D:BG;F:H;G:DEH;E:FGH;H:EG;C:AD. A C 3
A:BCD;B:AD;D:BG;F:H;G:DEH;E:FGH;H:EG;C:AD. A C 1
A:DBC. A B 20
A:DBC. A B 1
A:BC:A A B 1
#
Code: Select all
D F G /E
A B C D G E F /H
C D /B
A B D /C
/B
A /C
/B
D F G /E
A B D G E F /H
/D
A /D
/A
Hello, please explains why the answer is /D? If I understand the problem correctly, the graph should be like this:rickyliu wrote:Try this:Your answer is:Code: Select all
A:DBC. A B 20 #
The correct answer should be:Code: Select all
/C
Code: Select all
/D
So Minotaur should go to C (because C < D) and be trapped there immediately?where in this case the Minotaur checks the exits from a cavern in alphabetical order
Code: Select all
A:DBC. A B 20
#
Thank you, I got it!brianfry713 wrote:Read the problem description again. It was alphabetical just for the sample case. "The description of a labyrinth will identify each cavern by an upper case character and will list the caverns reachable from that cavern in the order that the Minotaur will attempt them"
In the inputThe Minotaur will attempt to go from A first to D, then B, then C.Code: Select all
A:DBC. A B 20 #
Code: Select all
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
//===============================================//
// Inner Classes
//===============================================//
public class ArrayListInteger {
public ArrayList<Integer> value = new ArrayList<Integer>();
}
//===============================================//
// Variables
//===============================================//
private static ArrayListInteger[] mAdjacentVertexes;
private static boolean[] mVertexDisabled;
private static int mFinalVertex = 0;
private static int mThesusVertex = 0;
private static int mMinotaurVertex = 0;
private static int mK = 0;
//===============================================//
// Public Methods
//===============================================//
public static void parseInput(String pRawData) {
String[] periodDelimiter = pRawData.split("\\.");
String[] passages = periodDelimiter[0].split(";");
for (int i = 0; i < passages.length; i++) {
int startVertex = passages[i].charAt(0) - 'A';
if (startVertex > mFinalVertex) mFinalVertex = startVertex;
for (int j = 2; j < passages[i].length(); j++) {
int endVertex = passages[i].charAt(j) - 'A';
if (endVertex > mFinalVertex) mFinalVertex = endVertex;
mAdjacentVertexes[startVertex].value.add(endVertex);
}
}
mFinalVertex++;
String initData[] = periodDelimiter[1].trim().split(" ");
mMinotaurVertex = initData[0].charAt(0) - 'A';
mThesusVertex = initData[1].charAt(0) - 'A';
mK = Integer.parseInt(initData[2]);
}
public static String traverse() {
String result = "";
int mTravelCount = 0;
int nextVertex = -1;
do {
nextVertex = -1;
for (int i = 0; i < mAdjacentVertexes[mMinotaurVertex].value.size(); i++) {
int currentVertex = mAdjacentVertexes[mMinotaurVertex].value.get(i);
if (mVertexDisabled[currentVertex] || currentVertex == mThesusVertex) continue;
nextVertex = currentVertex;
break;
}
if (nextVertex != -1) {
mTravelCount++;
if (mAdjacentVertexes[mThesusVertex].value.indexOf(mMinotaurVertex) != -1)
mThesusVertex = mMinotaurVertex;
mMinotaurVertex = nextVertex;
if (mTravelCount == mK) {
// Print result
result += (char)(mThesusVertex + 'A') + " ";
// Clear this vertex
mVertexDisabled[mThesusVertex] = true;
mTravelCount = 0;
}
}
} while (nextVertex != -1);
result += "/" + (char)(mMinotaurVertex + 'A');
return result;
}
public static void main(String[] args) {
String result = "";
Scanner scanner = new Scanner(System.in);
// Initialize
mAdjacentVertexes = new ArrayListInteger[26];
Main obj = new Main();
for (int i = 0; i < 26; i++) {
mAdjacentVertexes[i] = obj.new ArrayListInteger();
}
mVertexDisabled = new boolean[26];
String currentInput = scanner.nextLine();
while (!currentInput.equals("#")) {
// Reset value
for (int i = 0; i < 26; i++) {
mAdjacentVertexes[i].value.clear();
mVertexDisabled[i] = false;
}
mFinalVertex = 0;
// Input
parseInput(currentInput);
// Traverse
result += traverse() + "\n";
// Next line
currentInput = scanner.nextLine();
}
System.out.print(result);
}
}
Code: Select all
B:A;A:B. A C 999999999
#
Code: Select all
/B
So C doesn't have any connection with A and B, so Theseus cannot chase the Minotaur? Then if I try comment out this line:If a cavern has no exit it may or may not be in the input.
Code: Select all
if (mAdjacentVertexes[mThesusVertex].value.indexOf(mMinotaurVertex) != -1)
Code: Select all
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
char st[250];
long i,n,ts,mt,k,vis[35],c,sz;
vector<long>vc[35],op;
bool fg;
int main()
{
while(gets(st) && strcmp(st,"#"))
{
for(i=0;i<30;i++)
{
vc[i].clear();
vis[i]=0;
}
for(i=0;st[i];i++)
{
if(st[i]>='A' && st[i]<='Z')
{
n=st[i]-65;
i+=2;
while(st[i]>='A' && st[i]<='Z')
{
vc[n].push_back(st[i]-65);
i++;
}
}
if(st[i]=='.')
{
mt=st[i+2]-65;
ts=st[i+4]-65;
k=st[i+6]-'0';
break;
}
}
c=0;
while(1)
{
fg=0;
sz=vc[mt].size();
for(i=0;i<sz;i++)
{
n=vc[mt][i];
if(vis[n] || n==ts)
continue;
else
{
c++;
fg=1;
ts=mt;
mt=n;
if(c==k)
{
op.push_back(ts);
vis[ts]=1;
c=0;
}
break;
}
}
if(fg==0)
break;
}
sz=op.size();
for(i=0;i<sz;i++)
printf("%c ",op[i]+65);
printf("%c%c\n",47,mt+65);
op.clear();
}
return 0;
}
Code: Select all
A:BCD;B:AD;C:AD;D:B. A C 3
A:BCD;B:AD;D:BG;F:H;G:DEH;E:FGH;H:EG. A C 3
Code: Select all
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
char st[250];
long i,n,ts,mt,k,vis[35],c,sz;
vector<long>vc[35],op;
bool fg;
int main()
{
while(gets(st) && strcmp(st,"#"))
{
for(i=0;i<30;i++)
{
vc[i].clear();
vis[i]=0;
}
for(i=0;st[i];i++)
{
if(st[i]>='A' && st[i]<='Z')
{
n=st[i]-65;
i+=2;
while(st[i]>='A' && st[i]<='Z')
{
vc[n].push_back(st[i]-65);
i++;
}
}
if(st[i]=='.')
{
mt=st[i+2]-65;
ts=st[i+4]-65;
k=st[i+6]-'0';
break;
}
}
c=0;
while(1)
{
fg=0;
sz=vc[mt].size();
for(i=0;i<sz;i++)
{
n=vc[mt][i];
if(vis[n] || n==ts)
continue;
else
{
c++;
fg=1;
ts=mt;
mt=n;
if(c==k)
{
op.push_back(ts);
vis[ts]=1;
c=0;
}
break;
}
}
if(fg==0)
break;
}
sz=op.size();
for(i=0;i<sz;i++)
printf("%c ",op[i]+65);
printf("%c%c\n",47,mt+65);
op.clear();
}
return 0;
}
Code: Select all
A:BCD;B:AD;C:AD;D:B. A C 3
A:BCD;B:AD;D:BG;F:H;G:DEH;E:FGH;H:EG. A C 3