srm 237 Div2 1000 pts


这题的500pts顺便提一下,500的纯暴力是不行的,这题主要用到了集合论的知识,一个元素和两个元素的都好做,三个元素的并集我倒忘记了公式,后来还查了一下关于三个集合的并集知识。

这题不是一般的恶心,关键是题意不明确,我是看了好久才看懂讲了什么意思。。。。
代码如下:
// Paste me into the FileEdit configuration dialog
import java.io.*;
import java.util.*;
import java.util.regex.*;
//srm 237 Div2 1000 pts

public class MirrorPath {
   public String[] path(String[] map) {
//	   	for(String s:map)
//	   		System.out.println(s);
		int row=map.length;
		int col=map[0].length();
//		System.out.println(row+" "+col);
		String[] ans=new String[row];
		char[][] chs=new char[row][col];
		Pos[] step=new Pos[4];
		step[0]=new Pos(1,0);
		step[1]=new Pos(0,-1);
		step[2]=new Pos(-1,0);
		step[3]=new Pos(0,1);
		Pos start=null;
		Pos end=null;
		for(int i=0;i<row;i++){
			chs[i]=map[i].toCharArray();
			for(int j=0;j<col;j++){
				if(chs[i][j]=='.'){
					if(start==null&&(i==0||i==row-1||j==0||j==col-1)){
						start=new Pos();
						start.x=i;
						start.y=j;
						makeDir(start,i,j,row,col);
					}else if(end==null&&(i==0||i==row-1||j==0||j==col-1)){
						end=new Pos();
						end.x=i;
						end.y=j;
					}	
				}
			}//for
		}//for
		
		LinkedList<Path> ll=new LinkedList<Path>();
		LinkedList<Path> all=new LinkedList<Path>();
		Path init=new Path(start);
		init.mirCor.add(start.x+" "+start.y+" "+start.dir);
		ll.add(init);
		while(ll.size()>0){
				Path path=ll.remove();
				Pos pos=path.ll.getLast();
				int nx=pos.x+step[pos.dir].x;
				int ny=pos.y+step[pos.dir].y;
				if(nx==end.x&&ny==end.y){
					path.ll.add(new Pos(nx,ny,pos.dir));
					all.add(path);
				}else if(nx>=0&&nx<row&&ny>=0&&ny<col){
						if(chs[nx][ny]=='/'||chs[nx][ny]=='`'){
							Path[] paths=makePath(path,pos,nx,ny,chs);
							for(Path p:paths){
								String mirCor=nx+" "+ny+" "+p.ll.getLast().dir;
								String mirCor2=nx+" "+ny+" "+(p.ll.getLast().dir+2)%4;
								if(!p.mirCor.contains(mirCor)&&!p.mirCor.contains(mirCor2)){
									ll.add(p);
									p.mirCor.add(mirCor);
								}
							}
						}else{
							String mirCor=nx+" "+ny+" "+pos.dir;
							String mirCor2=nx+" "+ny+" "+(pos.dir+2)%4;
							if(!path.mirCor.contains(mirCor)&&!path.mirCor.contains(mirCor2)){
								path.ll.add(new Pos(nx,ny,pos.dir));
								ll.add(path);
								path.mirCor.add(mirCor);
							}
						}//else
				}
		}//while
		
		for(Path p:all){
//			print(p,chs);
			for(Pos pos:p.ll){
				int x=pos.x;
				int y=pos.y;
				switch(chs[x][y]){
					case '.':
						if(pos.dir==0||pos.dir==2)
							chs[x][y]='|';
						else
							chs[x][y]='-';	
						break;
					case '|':
						if(pos.dir==1||pos.dir==3)
							chs[x][y]='+';
						 break;
					case '-':
						if(pos.dir==0||pos.dir==2)
							chs[x][y]='+';
						break;
					default:
						break;	
				}
			}//for
		}//for
		
		for(int i=0;i<row;i++){
			ans[i]=new String(chs[i]);
		}
		
		return ans;
   }
   
   private void print(Path path,char[][] chs) {
	   boolean printed=false;
	   for(Pos p:path.ll){
		   if(p.x==1&&p.y==44){
			   printed=true;
			   break;
		   }
	   }
	   
	   if(!printed)
		   	return;
	  
	   for(Pos p:path.ll){
		   System.out.println(p);
	   }
//	System.out.println("///////////////////////////////////////////////////");
//	   for(Pos p:path.ll)
//		   System.out.println(p);
//		   
//		   System.out.println("///////////////////////////////////////////////////");
//	   System.out.println();
	   System.exit(0);
	
}

public Path[] makePath(Path path,Pos pos,int nx,int ny,char[][] chs){
   	Vector<Path> vec=new Vector<Path>();
   		if(pos.dir==0||pos.dir==2){
   			//case 1
   			int dir=3;
   			Path p=new Path();
   			for(String s:path.mirCor.toArray(new String[0]))
   				p.mirCor.add(s);
			for(Pos p1:path.ll){
				p.ll.add(p1);
			}
			p.ll.add(new Pos(nx,ny,dir));
			vec.add(p);
   			
			//case 2
   			dir=1;
   			p=new Path();
   			for(String s:path.mirCor.toArray(new String[0]))
   				p.mirCor.add(s);
			for(Pos p1:path.ll){
				p.ll.add(p1);
			}
			p.ll.add(new Pos(nx,ny,dir));
			vec.add(p);
   		}else{
   			//case 1
   			int dir=0;
   			Path p=new Path();
   			for(String s:path.mirCor.toArray(new String[0]))
   				p.mirCor.add(s);
			for(Pos p1:path.ll){
				p.ll.add(p1);
			}
			p.ll.add(new Pos(nx,ny,dir));
			vec.add(p);
   			
   			//case 2
   			dir=2;
   			p=new Path();
   			for(String s:path.mirCor.toArray(new String[0]))
   				p.mirCor.add(s);
			for(Pos p1:path.ll){
				p.ll.add(p1);
			}
			p.ll.add(new Pos(nx,ny,dir));
   			vec.add(p);
   		}
   		
   		return vec.toArray(new Path[0]);
   }
   
  class Path{
  	LinkedList<Pos>	ll=new 	LinkedList<Pos>();
  	HashSet<String> mirCor=new HashSet<String>();
  	public Path(Pos cur){
  		ll.add(cur);
  	}
  	public Path(){}
  }
   
   public void makeDir(Pos pos,int i,int j,int row,int col){
	 	//clockwise
	   if(i==0)
	   	pos.dir=0;//from the top shine		
	   else if(i==row-1)
	   	pos.dir=2;//from the buttom
	   else if(j==0)
	   	pos.dir=3;//the left		
	   else if(j==col-1)
	   	pos.dir=1;//the right	
   }
   
   class Pos{
   	int x;
   	int y;	
   	int dir=-1;
   	
   	public Pos(){}
   	public Pos(int x,int y){
   		this.x=x;
   		this.y=y;
   	}
   	public Pos(int x,int y,int dir){
   		this(x,y);
   		this.dir=dir;	
   	}
  
   	public String toString() {
   		return x+" "+y+" "+dir;
   	}
   }


// BEGIN CUT HERE
   public static void main(String[] args) {
		if (args.length == 0) {
			MirrorPathHarness.run_test(-1);
		} else {
			for (int i=0; i<args.length; ++i)
				MirrorPathHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class MirrorPathHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(String[] expected, String[] result) { if (expected.length != result.length) return false; for (int i=0; i<expected.length; ++i) if (!expected[i].equals(result[i])) return false; return true; }

	static String formatResult(String[] res) {
		String ret = "";
		ret += "{";
		for (int i=0; i<res.length; ++i) {
			if (i > 0) ret += ",";
			ret += String.format(" \"%s\"", res[i]);
		}
		ret += " }";
		return ret;
	}
	
	static int verifyCase(int casenum, String[] expected, String[] received) { 
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
//			System.out.println("//////////////////////");
//			for(String s:expected)
//				System.out.println(s);
//			System.out.println("//////////////////////");	
//			
//			System.out.println("*********************");
//			for(String s:received)
//				System.out.println(s);
//			System.out.println("*********************");
			
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch(casenum) {
//		case 0: {
//			String[] map              = { "#.#",   "#.#",   "#.#" } ;
//			String[] expected__       = {"#|#", "#|#", "#|#" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}
//		case 1: {
//			String[] map              = { "############",   "#######/....",   "######//####",   "#####//#####",   "####//######",   "..../#######",   "############" };
//			String[] expected__       = {"############", "#######/----", "######//####", "#####//#####", "####//######", "----/#######", "############" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}
//		case 2: {
//			String[] map              = { "#######",   "##/..`#",   "##.##.#",   "##.##.#",   "...../#",   "##.####",   "##.####" };
//			String[] expected__       = {"#######", "##/--`#", "##|##|#", "##|##|#", "--+--/#", "##|####", "##|####" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}
//		case 3: {
//			String[] map              = { "###########.#",   "#/........./.",   "#.#########.#",   "#`........./#",   "#############" };
//			String[] expected__       = {"###########|#", "#/---------/-", "#|#########|#", "#`---------/#", "#############" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}
//		case 4: {
//			String[] map              = { "########.##",   "#./......`#",   "#../.`....#",   "#.`...../.#",   "#....`.../#",   "###.#######" };
//			String[] expected__       = {"########|##", "#./-----+`#", "#.|/-`..||#", "#.`+-+--/|#", "#..|.`---/#", "###|#######" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}
//		case 5: {
//			String[] map              = { "##.########",   "#.........#",   "..`.`.....#",   "#..`......#",   "#.`.`.`...#",   "#....`....#",   "#...`.`.`.#",   "#.........#",   "#.....`./.#",   "#.........#",   "###########" };
//			String[] expected__       = {"##|########", "#.|.......#", "--`-`.....#", "#.|`|.....#", "#.`-`-`...#", "#...|`|...#", "#...`-`-`.#", "#.....|.|.#", "#.....`-/.#", "#.........#", "###########" };
//
//			return verifyCase(casenum, expected__, new MirrorPath().path(map));
//		}

		// custom cases

      case 6: {
			String[] map              = {"##################################################", "#.........../.........`....................../..`#", "#..../........................................../#", "#............................................`..`#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#./.................../............`.............#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#.`../................`........................./#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#................................................#", "#.....................`............/.............#", "#....`............................................", "#................................................#", "#................................................#", "############.#####################################"};
			String[] expected__       = {"##################################################", "#.........../---------`....................../--`#", "#..../------+---------+----------------------+--/#", "#....|......|.........|......................`--`#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#....|......|.........|.........................|#", "#./--+------+---------/------------`............|#", "#.|..|......|.........|............|............|#", "#.|..|......|.........|............|............|#", "#.|..|......|.........|............|............|#", "#.|..|......|.........|............|............|#", "#.`--/------+---------`------------+------------/#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........|............|.............#", "#....|......|.........`------------/.............#", "#....`------+-------------------------------------", "#...........|....................................#", "#...........|....................................#", "############|#####################################"};

			return verifyCase(casenum, expected__, new MirrorPath().path(map));
		}
/*      case 7: {
			String[] map              = ;
			String[] expected__       = ;

			return verifyCase(casenum, expected__, new MirrorPath().path(map));
		}*/
/*      case 8: {
			String[] map              = ;
			String[] expected__       = ;

			return verifyCase(casenum, expected__, new MirrorPath().path(map));
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE



不知道什么原因还是有点问题,明天继续写

你可能感兴趣的:(J#)