Az alábbi letöltési lehetőségek közül választhatsz: (
segítség)
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: utf-8
Méret: 4 KB
package vasut;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Vasut {
final static int MAXCODE = 1024;
boolean states[][];
int cur, prev;
String strCode0(int j, int nn) {
String res = "";
while (j != 0) {
if (j % 2 == 1) res = "1"+res;
else res = "0"+res;
nn--;
j = j/2;
}
while (nn > 0) {
res = "0"+res;
nn--;
}
return res;
}
String strCode(int stateACD) {
return strCode0(stateACD % 4, 2)
+" "+strCode0((stateACD / 4) % 16, 4)
+" "+strCode0((stateACD / 64) % 16, 4);
}
int code(int A, int C, int D) {
return A+(C<<2)+(D<<6);
}
@return
int minStack(int A) {
int min = 0;
while (min < 4 && (A>>min & 1) == 0) {
min++;
}
return min;
}
void nextStates(int stateACD, int vagon) {
int A, C, D;
A = stateACD % 4;
stateACD = stateACD >> 2;
C = stateACD % 16;
stateACD = stateACD >> 4;
D = stateACD % 16;
stateACD = stateACD >> 4;
if (stateACD > 0) throw new RuntimeException("eeeeeeeeeee");
for (int flush = A; flush < 4; flush++) {
if (((C|D) & 1<<flush) != 0) {
C = C & ~(1<<flush);
D = D & ~(1<<flush);
A = flush;
}
if (vagon >= A) {
states[cur][ code(vagon,C,D) ] = true;
if (minStack(C) >= vagon) {
states[cur][ code(A, C | 1<<vagon, D) ] = true;
}
if (minStack(D) >= vagon) {
states[cur][ code(A, C, D | 1<<vagon) ] = true;
}
}
}
}
@return
boolean solve(String s) {
states = new boolean[2][MAXCODE];
cur = 0;
prev = 1;
states[prev][code(0, 0, 0)] = true;
for (int i = 0; i < s.length(); i++) {
int ch = s.charAt(i);
if (ch < '1' || ch > '4') continue;
ch = ch-'1';
for (int j = 0; j < MAXCODE; j++) {
if (states[prev][j]) {
nextStates(j, ch);
}
states[prev][j] = false;
}
cur = 1-cur; prev = 1-prev;
}
for (int i = 0; i < MAXCODE; i++) {
if (states[prev][i]) return true;
}
return false;
}
void run(int id) throws IOException {
PrintWriter pw = new PrintWriter("vasut2.ki"+id);
BufferedReader br =
new BufferedReader(new FileReader("vasut.be"+id));
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
boolean res = solve(br.readLine());
if (res) pw.println("IGEN");
else pw.println("NEM");
}
br.close();
pw.close();
}
void run_all() throws IOException {
for (int i = 1; i <= 8; i++) {
run(i);
}
}
public static void main(String[] args) throws IOException {
new Vasut().run_all();
}
}