zoj1002 Fire Net

原创 2006年06月14日 20:23:00
  1. //zoj1002 Fire Net

  2. //Accepted 1002 C++ 00:00.01 436K

  3. #include <stdio.h>

  4. int n;

  5. int max;

  6. char map[5][5];

  7. bool canput(int x, int y)

  8. {

  9.        int i,j;

  10.        if (map[x][y]!='.') return false;

  11.        for (i=x+1; i<n; i++){

  12.               if (map[i][y]=='X') break;

  13.               if (map[i][y]=='O') return false;

  14.        }

  15.        for (i=x-1; i>=0; i--){

  16.               if (map[i][y]=='X') break;

  17.               if (map[i][y]=='O') return false;

  18.        }

  19.        for (j=y+1; j<n; j++){

  20.               if (map[x][j]=='X') break;

  21.               if (map[x][j]=='O') return false;

  22.        }

  23.        for (j=y-1; j>=0; j--){

  24.               if (map[x][j]=='X') break;

  25.               if (map[x][j]=='O') return false;

  26.        }

  27.        return true;

  28. }

  29. bool finish()

  30. {

  31.        for (int i=0; i<n; ++i)

  32.               for (int j=0; j<n; ++j)

  33.                      if (canput(i,j))

  34.                             return false;

  35.        return true;

  36. }

  37. void backtrack(int num)

  38. {

  39.        if (finish()){

  40.               if (num>max) max = num;

  41.               return ;

  42.        }

  43.        for (int i=0; i<n; ++i)

  44.               for (int j=0; j<n; ++j)

  45.                      if (canput(i,j)){

  46.                             map[i][j] = 'O';

  47.                             backtrack(num+1);

  48.                             map[i][j] = '.';

  49.                      }

  50. }

  51. void solve()

  52. {

  53.        getchar();

  54.        for (int i=0; i<n; ++i){

  55.               for (int j=0; j<n; ++j)

  56.                      scanf ("%c",&map[i][j]);

  57.               getchar();

  58.        }

  59.        max = 0;

  60.        backtrack(0);

  61.        printf ("%d/n",max);

  62. }

  63. int main()

  64. {

  65. #ifdef ONLINE_JUDGE

  66. #else

  67.        freopen("1002.txt","r",stdin);

  68. #endif

  69.        while (scanf("%d",&n)!=EOF && n)

  70.               solve();

  71. #ifdef ONLINE_JUDGE

  72. #else

  73.        fclose(stdin);

  74. #endif

  75.        return 0;

  76. }

ZOJ1002 Fire Net DFS(深度优先搜索) 已AC

Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with...

杭电1045(zoj1002)Fire Net

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S...
  • z8110
  • z8110
  • 2015年08月04日 18:48
  • 420

zoj 1002 Fire Net dfs

做完这道题,我发现自己越来越喜欢递归了。最初就是理解不了,现在却觉得用递归解决问题非常方便,哈哈! 最初这道题一会就把代码写好了,样例也过了,但就是WA。。。想了又想,还是没找出错误,最后搜了一下这...

fire net(zoj1002)

#include using namespace std; int n,maxB; char shape[100][100]; bool can(int i,int j) { int m,k; f...

ACM ZOJ Problem Set - 1002 fire net

   #include #include using namespace std; char a[4][4]; int b[16]; int main() { int n, i, j...

【图论】【二分图匹配】[ZOJ 1002]Fire Net

这道题目就是对不同的方向的同一行或者列的连通块进行标号,然后对方向不同且相交的连通快连边,最后做一次二分图最大匹配就好了。#include #include #include //#includ...

ZOJ Problem Set - 1002 Fire Net

Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with st...
  • Kexiii
  • Kexiii
  • 2016年04月17日 17:43
  • 267

ZOJ——1002 Fire Net

Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square ci...

ZOJ 1002 Fire Net

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 一道搜索的题目,数据量不是很大用深搜。判定一个点是不是能否放置碉堡,搜索其上...

ZOJ 1002 fire net DFS

Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city wi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:zoj1002 Fire Net
举报原因:
原因补充:

(最多只允许输入30个字)