Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package solution368;
- import java.io.*;
- import java.util.*;
- public class Main {
- StreamTokenizer in;
- PrintWriter out;
- public static void main(String[] args) throws Exception {
- new Main().run();
- }
- public void run() throws Exception {
- //in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
- in=new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
- //out= new PrintWriter(new OutputStreamWriter(System.out));
- out=new PrintWriter("output.txt");
- solve();
- out.flush();
- }
- int nextInt() throws Exception {
- in.nextToken();
- return (int) in.nval;
- }
- String next() throws Exception {
- in.nextToken();return in.sval;
- }
- public void solve() throws Exception {
- int n=nextInt();
- in.ordinaryChars('0','9');
- in.wordChars('0', '9');
- int [][] a = new int [n+1][n+1];
- int [][] b = new int [n+1][n+1];
- char [][] c = new char [n+1][n+1];
- for (int i=0; i<n;i++)
- for (int j=0; j<n;j++)
- c[i][j]='.';
- c[0][0]='#';
- c[n-1][n-1]='#';
- for (int i=0;i<n;i++)
- {
- String s =next() ;
- for (int j=0; j<n;j++)
- a[i][j]=s.charAt(j)-'0';
- }
- b[0][0]=a[0][0];
- for (int i=1; i<n;i++)
- {
- b[i][0]=b[i-1][0]+a[i][0];
- }
- for (int i=1; i<n;i++)
- {
- b[0][i]=b[0][i-1]+a[0][i];
- }
- for (int i=1; i<n;i++)
- for (int j=1; j<n;j++)
- {
- b[i][j]=Math.min(b[i-1][j], b[i][j-1])+a[i][j];
- }
- int i=n-1;
- int j=n-1;
- while (i!=0&&j!=0)
- {
- if (b[i][j-1]<b[i-1][j]) {c[i][j-1]='#';j--;}
- else {c[i-1][j]='#';i--;}
- }
- if (i==0)
- for (int k=j;k>=0;k--)
- c[0][k]='#';
- if (j==0)
- for (int k=i;k>=0;k--)
- c[k][0]='#';
- for (int k=0;k<n;k++)
- {
- for (int l=0; l<n;l++)
- out.print(c[k][l]);
- out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement