ZOJ 1002 Fire Net(dfs)

嗯...

题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364501

这道题是想出来则是一道很简单的dfs:

将一个4*4的地图给每一个点排序,如下图:

0  1  2  3

4  5  6  7

8  9  10  11

12 13 14 15

设一个点为第k个点,那么它的坐标为(k/n,k%n),根据这个进行dfs,当k == n * n是退出dfs。如果k < n *n,就继续dfs,判断是否能放下,即要放的这个点为空地并且横、纵上都没有东西,最后注意回溯。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 int n, ans;
 8 char g[5][5];
 9
10 inline bool canput(int x, int y){
11     for(int i = x - 1; i >= 0 && g[i][y] != ‘X‘; i--){
12         if(g[i][y] == ‘O‘) return 0;
13     }
14     for(int i = y - 1; i >= 0 && g[x][i] != ‘X‘; i--){
15         if(g[x][i] == ‘O‘) return 0;
16     }
17     return 1;
18 }
19
20 inline void dfs(int k, int cnt){
21     int x, y;
22     if(k == n * n){
23         if(cnt > ans) ans = cnt;
24         return;
25     }
26     else{
27         x = k / n;
28         y = k % n;
29         if(g[x][y] == ‘.‘ && canput(x, y)){
30             g[x][y] = ‘O‘;
31             dfs(k + 1, cnt + 1);
32             g[x][y] = ‘.‘;
33         }
34         dfs(k + 1, cnt);
35     }
36 }
37
38 int main(){
39     while(~scanf("%d", &n) && n){
40         ans = 0;
41         for(int i = 0; i < n; i++){
42             for(int j = 0; j < n; j++){
43                 cin >> g[i][j];
44             }
45         }
46         dfs(0, 0);
47         printf("%d\n", ans);
48     }
49     return 0;
50 }

AC代码

原文地址:https://www.cnblogs.com/New-ljx/p/11515316.html

时间: 2024-08-01 14:43:58

ZOJ 1002 Fire Net(dfs)的相关文章

hdu 1045 &amp;&amp; zoj 1002 Fire Net(DFS &amp;&amp; 二分图匹配)

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7958    Accepted Submission(s): 4542 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

ZOJ 1002 Fire Net(DFS啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a

HDU1045 Fire Net(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8185    Accepted Submission(s): 4691 Problem Description Suppose that we have a squa

Fire Net(dfs)

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7944    Accepted Submission(s): 4534 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

ZOJ 题目2100 Seeding(DFS)

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-ma

TOJ 1162 Fire Net(dfs)

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The fo

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

ZOJ 2477 Magic&#160;Cube(魔方)

ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 * 3 small cubes. We can unwrap the cube, it will become like this: 这是个有名的儿童游戏.游戏