http://acm.pku.edu.cn/JudgeOnline/problem?id=3620 原題
題目大意就是在一個N*M的網格區域內,找出有相同特徵的最大的一塊區域
具體實現用遞歸,但效率不是很高
下載是源碼(java):
- import java.io.*;
- import java.util.*;
- public class Main
- {
- static int[][] grids;
- public static void main(String[] args) throws Exception
- {
- readFile();
- for(int i=1; i<grids.length; i++)
- {
- for(int j=1; j<grids[i].length; j++)
- {
- if(grids[i][j]==1)
- {
- process(i,j);
- if(tempSum>sum)
- sum = tempSum;
- tempSum = 0;
- }
- }
- }
- System.out.print(sum);
- }
- static void readFile() throws Exception
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine()," ");
- int row = Integer.valueOf(st.nextToken());
- int col = Integer.valueOf(st.nextToken());
- int k = Integer.valueOf(st.nextToken());
- grids = new int[row+1][col+1];
- int count = 0;
- while(count<k)
- {
- st = new StringTokenizer(br.readLine()," ");
- grids[Integer.valueOf(st.nextToken())][Integer.valueOf(st.nextToken())] = 1;
- count++;
- }
- br.close();
- }
- static int tempSum = 0;
- static int sum = -1;
- static void process(int x,int y)
- {
- if(x<1 || x>=grids.length || y<1 || y>=grids[0].length)
- return;
- if(grids[x][y]!=1)
- return;
- tempSum++;
- grids[x][y] = 0;
- process(x-1,y);
- process(x,y+1);
- process(x+1,y);
- process(x,y-1);
- }
- }