Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main2 {
  5.     static final StdIn in = new StdIn();
  6.     static final PrintWriter out = new PrintWriter(System.out);
  7.    
  8.     public static void main(String[] args) {
  9.         for(int i=7; i<=10; ++i) {
  10.             System.out.println("Running test "+i);
  11.             try { /change the file paths here input & output
  12.                 new Solver("C:/Users/20113766/Desktop/sets/path"+String.format("%02d", i)+".in", "C:/Users/20113766/Desktop/out/path"+String.format("%02d", i)+".ans");
  13.             } catch(Exception e) {
  14.                 System.out.println("Exception on test "+i);
  15.                 e.printStackTrace();
  16.             }
  17.         }
  18.     }
  19.    
  20.     static class Solver {
  21.         StdIn in;
  22.         PrintWriter out;
  23.         Solver(String inf, String outf) throws Exception {
  24.             in = new StdIn(new FileInputStream(inf));
  25.             out = new PrintWriter(outf);
  26.             int t=in.nextInt();
  27.             while(t-->0) {
  28.                 solve();
  29.                 System.out.println(t+" tests left in this file");
  30.                 out.flush();
  31.             }
  32.             out.close();
  33.         }
  34.        
  35.         int m, n;
  36.         int[][] v;
  37.         static final int[][] stp = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  38.         static final char[] stpc = {'S', 'E', 'N', 'W'};
  39.         int ans1=-1;
  40.         String ans2="blahalhfilesjldi";
  41.         void solve() {
  42.             m=in.nextInt();
  43.             n=in.nextInt();
  44.             v = new int[n][m];
  45.             for(int i=0; i<n; ++i)
  46.                 for(int j=0; j<m; ++j)
  47.                     v[i][j]=in.nextInt();
  48.             solve2(new int[4], 0);
  49.             if(ans1==-1)
  50.                 System.out.println("THIS TEST FAILED INVALID ANSWER");
  51.             out.println(ans2);
  52.             out.println(ans1);
  53.         }
  54.         void solve2(int[] p, int pi) {
  55.             if(pi<4) {
  56.                 for(int i=0; i<4; ++i) {
  57.                     p[pi]=i;
  58.                     for(int j=0; j<pi&&p[pi]!=-1; ++j)
  59.                         if(p[j]==p[pi])
  60.                             p[pi]=-1;
  61.                     if(p[pi]!=-1)
  62.                         solve2(p, pi+1);
  63.                 }
  64.                 return;
  65.             }
  66.             boolean[][] vis = new boolean[n][m];
  67.             for(int si1=0; si1<n; ++si1) {
  68.                 for(int sj1=0; sj1<m; ++sj1) {
  69.                     int si=si1, sj=sj1, x=1, nv=1;
  70.                     for(int i=0; i<n; ++i)
  71.                         Arrays.fill(vis[i], false);
  72.                     vis[si][sj]=true;
  73.                     StringBuilder ts=new StringBuilder((sj+1)+" "+(si+1)+"\n");
  74.                     while(nv<n*m) {
  75.                         int d=0;
  76.                         for(; d<4; ++d) {
  77.                             int ni=si+stp[p[d]][0], nj=sj+stp[p[d]][1];
  78.                             if(ni>=0&&ni<n&&nj>=0&&nj<m&&!vis[ni][nj]) {
  79.                                 si=ni;
  80.                                 sj=nj;
  81.                                 break;
  82.                             }
  83.                         }
  84.                         if(d==4)
  85.                             break;
  86.                         ts.append(stpc[p[d]]);
  87.                         x+=x%v[si][sj];
  88.                         ++nv;
  89.                         vis[si][sj]=true;
  90.                     }
  91.                     if(nv<n*m)
  92.                         continue;
  93.                     if(x>ans1) {
  94.                         ans1=x;
  95.                         ans2=ts.toString();
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101.    
  102.     interface Input {
  103.         public String next();
  104.         public String nextLine();
  105.         public int nextInt();
  106.         public long nextLong();
  107.         public double nextDouble();
  108.     }
  109.     static class StdIn implements Input {
  110.         final private int BUFFER_SIZE = 1 << 16;
  111.         private DataInputStream din;
  112.         private byte[] buffer;
  113.         private int bufferPointer, bytesRead;
  114.         public StdIn() {
  115.             din = new DataInputStream(System.in);
  116.             buffer = new byte[BUFFER_SIZE];
  117.             bufferPointer = bytesRead = 0;
  118.         }
  119.         public StdIn(InputStream in) {
  120.             try{
  121.                 din = new DataInputStream(in);
  122.             } catch(Exception e) {
  123.                 throw new RuntimeException();
  124.             }
  125.             buffer = new byte[BUFFER_SIZE];
  126.             bufferPointer = bytesRead = 0;
  127.         }
  128.         public String next() {
  129.             int c;
  130.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  131.             StringBuilder s = new StringBuilder();
  132.             while (c != -1)
  133.             {
  134.                 if (c == ' ' || c == '\n'||c=='\r')
  135.                     break;
  136.                 s.append((char)c);
  137.                 c=read();
  138.             }
  139.             return s.toString();
  140.         }
  141.         public String nextLine() {
  142.             int c;
  143.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  144.             StringBuilder s = new StringBuilder();
  145.             while (c != -1)
  146.             {
  147.                 if (c == '\n'||c=='\r')
  148.                     break;
  149.                 s.append((char)c);
  150.                 c = read();
  151.             }
  152.             return s.toString();
  153.         }
  154.         public int nextInt() {
  155.             int ret = 0;
  156.             byte c = read();
  157.             while (c <= ' ')
  158.                 c = read();
  159.             boolean neg = (c == '-');
  160.             if (neg)
  161.                 c = read();
  162.             do
  163.                 ret = ret * 10 + c - '0';
  164.             while ((c = read()) >= '0' && c <= '9');
  165.  
  166.             if (neg)
  167.                 return -ret;
  168.             return ret;
  169.         }
  170.         public int[] readIntArray(int n) {
  171.             int[] ar = new int[n];
  172.             for(int i=0; i<n; ++i)
  173.                 ar[i]=nextInt();
  174.             return ar;
  175.         }
  176.         public long nextLong() {
  177.             long ret = 0;
  178.             byte c = read();
  179.             while (c <= ' ')
  180.                 c = read();
  181.             boolean neg = (c == '-');
  182.             if (neg)
  183.                 c = read();
  184.             do
  185.                 ret = ret * 10 + c - '0';
  186.             while ((c = read()) >= '0' && c <= '9');
  187.             if (neg)
  188.                 return -ret;
  189.             return ret;
  190.         }
  191.         public long[] readLongArray(int n) {
  192.             long[] ar = new long[n];
  193.             for(int i=0; i<n; ++i)
  194.                 ar[i]=nextLong();
  195.             return ar;
  196.         }
  197.         public double nextDouble() {
  198.             double ret = 0, div = 1;
  199.             byte c = read();
  200.             while (c <= ' ')
  201.                 c = read();
  202.             boolean neg = (c == '-');
  203.             if (neg)
  204.                 c = read();
  205.             do
  206.                 ret = ret * 10 + c - '0';
  207.             while ((c = read()) >= '0' && c <= '9');
  208.             if (c == '.')
  209.                 while ((c = read()) >= '0' && c <= '9')
  210.                     ret += (c - '0') / (div *= 10);
  211.             if (neg)
  212.                 return -ret;
  213.             return ret;
  214.         }
  215.         private void fillBuffer() throws IOException {
  216.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  217.             if (bytesRead == -1)
  218.                 buffer[0] = -1;
  219.         }
  220.         private byte read() {
  221.             try{
  222.                 if (bufferPointer == bytesRead)
  223.                     fillBuffer();
  224.                 return buffer[bufferPointer++];
  225.             } catch(IOException e) {
  226.                 throw new RuntimeException();
  227.             }
  228.         }
  229.         public void close() throws IOException {
  230.             if (din == null)
  231.                 return;
  232.             din.close();
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement