[JAVA][ZOJ 1002][Fire Net]

  1. import java.io.BufferedInputStream;  
  2. import java.util.Scanner;  
  3.   
  4.   
  5. public class Main {  
  6.   
  7.   
  8.     static char pos[][] = new char[4][4];  
  9.     static int numOfLegalPos = 0;  
  10.     static int maxNumOfLegalPos = 0;  
  11.     static int n;  
  12.   
  13.   
  14.     public static boolean check(int y, int x) {  
  15.         for (int i = x + 1; i < n; i++) {// check right dir  
  16.             if (pos[y][i] == 'X') {  
  17.                 break;  
  18.             } else if (pos[y][i] == '0') {// if there is a wall in the same line,return false means there cant put a city  
  19.                 return false;  
  20.             }  
  21.         }  
  22.         for (int i = x - 1; i >= 0; i--) {// check left dir  
  23.             if (pos[y][i] == 'X') {  
  24.                 break;  
  25.             } else if (pos[y][i] == '0') {  
  26.                 return false;  
  27.             }  
  28.         }  
  29.         for (int i = y + 1; i < n; i++) {// check down dir  
  30.             if (pos[i][x] == 'X') {  
  31.                 break;  
  32.             } else if (pos[i][x] == '0') {  
  33.                 return false;  
  34.             }  
  35.         }  
  36.         for (int i = y - 1; i >= 0; i--) {// check up dir  
  37.             if (pos[i][x] == 'X') {  
  38.                 break;  
  39.             } else if (pos[i][x] == '0') {  
  40.                 return false;  
  41.             }  
  42.         }  
  43.         return true;  
  44.     }  
  45.   
  46.   
  47.     public static void dfs(int n) {  
  48.         if (numOfLegalPos > maxNumOfLegalPos) {  
  49.             maxNumOfLegalPos = numOfLegalPos;  
  50.         }  
  51.         for (int y = 0; y < n; y++) {  
  52.             for (int x = 0; x < n; x++) {  
  53.                 if (pos[y][x] == 'X' || pos[y][x] == '0') {// if here is a wall or a city ingore it.  
  54.                     continue;  
  55.                 } else if (check(y, x)) {// if here can put a city  
  56.                     pos[y][x] = '0';  
  57.                     numOfLegalPos++;  
  58.                     dfs(n);  
  59.                     pos[y][x] = '.';  
  60.                     numOfLegalPos--;  
  61.                 }  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.   
  67.     public static void main(String[] args) {  
  68.   
  69.   
  70.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  71.         n = sc.nextInt();// line of maze  
  72.         while (n != 0) {  
  73.             for (int i = 0; i < n; i++) {// transform strings into chars  
  74.                 String tmp = sc.next();  
  75.                 for (int j = 0; j < n; j++) {  
  76.                     pos[i][j] = tmp.charAt(j);  
  77.                 }  
  78.             }  
  79.             dfs(n);  
  80.             System.out.println(maxNumOfLegalPos);  
  81.             n = sc.nextInt();  
  82.             maxNumOfLegalPos=0;  
  83.         }  
  84.     }  
  85. }  

明明只是个初级题目,居然让自己忙了这么久。看来不扎实啊。。。

一开始的想法是:先将某个点设为blockhouse,然后按行检索其他所有位置是否可以放下blockhouse,但是用简单循环一直是WA。其中的错误在于。。如果用简单循环把第后面的位置设为blackhouse后,就忽略了前面位置已经设为blackhouse的状态。因此用dfs检索所有可能项,简单循环就会丢失一部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值