Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main2 {
- static final StdIn in = new StdIn();
- static final PrintWriter out = new PrintWriter(System.out);
- public static void main(String[] args) {
- for(int i=7; i<=10; ++i) {
- System.out.println("Running test "+i);
- try { /change the file paths here input & output
- new Solver("C:/Users/20113766/Desktop/sets/path"+String.format("%02d", i)+".in", "C:/Users/20113766/Desktop/out/path"+String.format("%02d", i)+".ans");
- } catch(Exception e) {
- System.out.println("Exception on test "+i);
- e.printStackTrace();
- }
- }
- }
- static class Solver {
- StdIn in;
- PrintWriter out;
- Solver(String inf, String outf) throws Exception {
- in = new StdIn(new FileInputStream(inf));
- out = new PrintWriter(outf);
- int t=in.nextInt();
- while(t-->0) {
- solve();
- System.out.println(t+" tests left in this file");
- out.flush();
- }
- out.close();
- }
- int m, n;
- int[][] v;
- static final int[][] stp = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- static final char[] stpc = {'S', 'E', 'N', 'W'};
- int ans1=-1;
- String ans2="blahalhfilesjldi";
- void solve() {
- m=in.nextInt();
- n=in.nextInt();
- v = new int[n][m];
- for(int i=0; i<n; ++i)
- for(int j=0; j<m; ++j)
- v[i][j]=in.nextInt();
- solve2(new int[4], 0);
- if(ans1==-1)
- System.out.println("THIS TEST FAILED INVALID ANSWER");
- out.println(ans2);
- out.println(ans1);
- }
- void solve2(int[] p, int pi) {
- if(pi<4) {
- for(int i=0; i<4; ++i) {
- p[pi]=i;
- for(int j=0; j<pi&&p[pi]!=-1; ++j)
- if(p[j]==p[pi])
- p[pi]=-1;
- if(p[pi]!=-1)
- solve2(p, pi+1);
- }
- return;
- }
- boolean[][] vis = new boolean[n][m];
- for(int si1=0; si1<n; ++si1) {
- for(int sj1=0; sj1<m; ++sj1) {
- int si=si1, sj=sj1, x=1, nv=1;
- for(int i=0; i<n; ++i)
- Arrays.fill(vis[i], false);
- vis[si][sj]=true;
- StringBuilder ts=new StringBuilder((sj+1)+" "+(si+1)+"\n");
- while(nv<n*m) {
- int d=0;
- for(; d<4; ++d) {
- int ni=si+stp[p[d]][0], nj=sj+stp[p[d]][1];
- if(ni>=0&&ni<n&&nj>=0&&nj<m&&!vis[ni][nj]) {
- si=ni;
- sj=nj;
- break;
- }
- }
- if(d==4)
- break;
- ts.append(stpc[p[d]]);
- x+=x%v[si][sj];
- ++nv;
- vis[si][sj]=true;
- }
- if(nv<n*m)
- continue;
- if(x>ans1) {
- ans1=x;
- ans2=ts.toString();
- }
- }
- }
- }
- }
- interface Input {
- public String next();
- public String nextLine();
- public int nextInt();
- public long nextLong();
- public double nextDouble();
- }
- static class StdIn implements Input {
- final private int BUFFER_SIZE = 1 << 16;
- private DataInputStream din;
- private byte[] buffer;
- private int bufferPointer, bytesRead;
- public StdIn() {
- din = new DataInputStream(System.in);
- buffer = new byte[BUFFER_SIZE];
- bufferPointer = bytesRead = 0;
- }
- public StdIn(InputStream in) {
- try{
- din = new DataInputStream(in);
- } catch(Exception e) {
- throw new RuntimeException();
- }
- buffer = new byte[BUFFER_SIZE];
- bufferPointer = bytesRead = 0;
- }
- public String next() {
- int c;
- while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
- StringBuilder s = new StringBuilder();
- while (c != -1)
- {
- if (c == ' ' || c == '\n'||c=='\r')
- break;
- s.append((char)c);
- c=read();
- }
- return s.toString();
- }
- public String nextLine() {
- int c;
- while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
- StringBuilder s = new StringBuilder();
- while (c != -1)
- {
- if (c == '\n'||c=='\r')
- break;
- s.append((char)c);
- c = read();
- }
- return s.toString();
- }
- public int nextInt() {
- int ret = 0;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do
- ret = ret * 10 + c - '0';
- while ((c = read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public int[] readIntArray(int n) {
- int[] ar = new int[n];
- for(int i=0; i<n; ++i)
- ar[i]=nextInt();
- return ar;
- }
- public long nextLong() {
- long ret = 0;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do
- ret = ret * 10 + c - '0';
- while ((c = read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public long[] readLongArray(int n) {
- long[] ar = new long[n];
- for(int i=0; i<n; ++i)
- ar[i]=nextLong();
- return ar;
- }
- public double nextDouble() {
- double ret = 0, div = 1;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do
- ret = ret * 10 + c - '0';
- while ((c = read()) >= '0' && c <= '9');
- if (c == '.')
- while ((c = read()) >= '0' && c <= '9')
- ret += (c - '0') / (div *= 10);
- if (neg)
- return -ret;
- return ret;
- }
- private void fillBuffer() throws IOException {
- bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
- if (bytesRead == -1)
- buffer[0] = -1;
- }
- private byte read() {
- try{
- if (bufferPointer == bytesRead)
- fillBuffer();
- return buffer[bufferPointer++];
- } catch(IOException e) {
- throw new RuntimeException();
- }
- }
- public void close() throws IOException {
- if (din == null)
- return;
- din.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement