ZOJ 1002 Fire Net Solution

来源:互联网 发布:思途cms 类似 编辑:程序博客网 时间:2024/05/18 02:20

Problem Specification

Link: Zhejiang University Oline Judge Problem Set, No. 1002

Solution

1. Mapping
Map the square city to a 4*4 2-D int array, with value  -1, 0, 1, 2 representing unused place, vacancy, wall and bullet covered place separately.
Attention: wall and bullet covered place should be designated to different identity numbers since their attributes are not identical. Bullet can't penetrate wall, but it can penetrate bullet covered place. Use only one identity number would cause a wrong result.
2. Deep Copy (Complete Copy)
In the Deep First Search algorithm which is used in this solution, a new 2-D array is constructed containing previous map data. Then modify this newly constructed array by setting a new castle at a vacancy and adding its bullet cover path. Then the newly constructed array is passed as parameter to a recursive search function of next layer. (Will be elaborated in part 3)
However, one tricky detail emerges when it comes to copying data from one array to another, especially high dimensional array. 
Let's begin our topic with one dimensional array, which is the simplest scenario. Then we analyse this problem step by step.
2.1 1-D Array Copy
Say, we want to copy data from 1-D array "map" to 1-D array "newmap".
 
int []map = {1, 2, 3, 4};
int []newmap;newmap = map;for(int i =0; i < newmap.length; i++)
newmap[i]++;
System.out.println(map);
System.out.println(newmap);
Wow! Both arrays become {2, 3, 4, 5}! But we only want to increase the values in newmap by one. Quite strange right? Well, the result seems reasonable considering the value passing way in Java.
Basically, there are two attributes in each variable: address and value. Either of them is passed in assignment statements.
/* Remember the three basic types of statement? * Assignment statement * Loop statement * Condition statement *  * We can regard a statement as an assignment statement as long as there is a equal mark in it explicitly. * Following are some examples (all of these are assignment statements)  **/int a = 1;int b = 3;int []map = newmap;a = b;a = b + a;a = Math.sqrt(b + 5);
For sake of convenience, we name the variable left to the equal mark as assigned variable, meanwhile name that right to the equal mark as assigning variable.
If both variables (assigned and assigning variable) are primitive data type, the value of assigning variable is passed to assigned variable. This is called pass-by-value.
If both variables are not primitive data type, the address of assigning variable is passed to assigned variable. This is called pass-by-reference.
/* * There are 8 primitive data types in Java, namely  * * byte: 8-bit signed two's complement integer. The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation. * * short: 16-bit signed two's complement integer. As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.         * * int: By default, the int data type is a 32-bit signed two's complement integer. Use the Integer class to use int data type as an unsigned integer.         *         * long: 64-bit two's complement integer.  * * float: The float data type is a single-precision 32-bit IEEE 754 floating point. Use float instead of double to save memory when accuracy is not quite needed. * * double: The double data type is a double-precision 64-bit IEEE 754 floating point. As mentioned above, this data type should never be used for precise values, such as currency.         *         * boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.         *         * char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). * * From: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html **/

Since int[] is not a primitive data type mentioned above. So its address is passed in assignment statement rather than value which is our expectation in this example. Actually, both variable "map" and "newmap" point to a same 1-D array object stored in heap. And both of them possess the right to modify it. But it is out of our current discussion.
In order to copy all the data in "map", a for loop is needed. Or we can call a class method of array clone();
int []map = {1, 2, 3, 4};int []newmap;newmap = new int[4];for(int i = 0; i < map.length; i++){newmap[i] = map[i];}/*Or call class method clone()*/newmap = map.clone();
2.2 2-D Array Copy
clone() function is not convenient enough. It can only copy one layer data in an array. It's confusing right? Let's see another code segment first.
int [][]map = {{1,2},{3,4}};int [][]newmap = new int[2][2];/*a for loop is needed.*/for(int i = 0 ; i < map.length; i++){newmap = map.clone();}
A for loop is needed when copying data from 2-D array. 
3. Depth First Search
To be continued.
4. Sample Solution in Java
import java.util.Scanner;public class Main{public static void main(String[] arg){Scanner reader = new Scanner(System.in);int size;int map[][] = new int[4][4];char c;int result;                /*Read input*/while((size = reader.nextInt()) != 0){                        /*This nextLine() function is to read and discard the newline character*/String temp = reader.nextLine();                        /*Initialize map array*/for(int i=0;i<4;i++)for(int j=0;j<4;j++)map[i][j]=-1;/*Fill data*/                        for(int row = 0; row < size;row++){String line = reader.nextLine();for(int column=0;column<size;column++)switch(c = line.charAt(column)){case '.':map[row][column] = 0;break;case 'X':map[row][column] = 1;break;}}/*read end*/result = 0;System.out.println(solve(map,size));}reader.close();}static int solve(int map[][],int size){if(size == 0) return 0;int result = 0;for(int row = 0; row < size; row++)for(int column = 0;column < size; column++){if(map[row][column] == 0){int tempmap[][] = new int[4][4];for(int i = 0 ; i < 4; i++)tempmap[i] = map[i].clone();tempmap[row][column] = 2;for(int tr=row+1;tr<size;tr++){if(tempmap[tr][column] == 1)break;tempmap[tr][column] = 2;}for(int tr=row-1;tr>=0;tr--){if(tempmap[tr][column] == 1)break;tempmap[tr][column] = 2;}for(int tc=column+1;tc<size;tc++){if(tempmap[row][tc] == 1)break;tempmap[row][tc] = 2;}for(int tc=column-1;tc>=0;tc--){if(tempmap[row][tc] == 1)break;tempmap[row][tc] = 2;}int re = solve(tempmap, size) + 1;if(re>result)result = re;}}return result;}}

                                             
0 0
原创粉丝点击