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,判断是否能放下,即要放的这个点为空地并且横、纵上都没有东西,最后注意回溯。
#include<cstdio>
#include<iostream>
#include<cstring> using namespace std; int n, ans;
char g[][]; inline bool canput(int x, int y){
for(int i = x - ; i >= && g[i][y] != 'X'; i--){
if(g[i][y] == 'O') return ;
}
for(int i = y - ; i >= && g[x][i] != 'X'; i--){
if(g[x][i] == 'O') return ;
}
return ;
} inline void dfs(int k, int cnt){
int x, y;
if(k == n * n){
if(cnt > ans) ans = cnt;
return;
}
else{
x = k / n;
y = k % n;
if(g[x][y] == '.' && canput(x, y)){
g[x][y] = 'O';
dfs(k + , cnt + );
g[x][y] = '.';
}
dfs(k + , cnt);
}
} int main(){
while(~scanf("%d", &n) && n){
ans = ;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
cin >> g[i][j];
}
}
dfs(, );
printf("%d\n", ans);
}
return ;
}
AC代码
ZOJ 1002 Fire Net(dfs)的更多相关文章
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- Fire Net(dfs)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1045 Fire Net(dfs)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- ZOJ 1008 Gnome Tetravex(DFS)
题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...
- ZOJ 2477 Magic Cube(魔方)
ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds Memory Limit: 65536 KB This is a very popular gam ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
随机推荐
- 番外:如何克隆可刷新的PDB(Refreshable PDB)
基于版本:19c (12.2.0.3) AskScuti 创建方法:克隆创建 对应路径:属于克隆.PDB类型为:Refreshable 相关系列请参考<Oracle创建PDB列表文章> 注 ...
- 优酷1080p的kux格式文件转码
@echo off for /r . %%i in (*.kux) do ( "C:\Program Files (x86)\YouKu\YoukuClient\nplayer\ffmpeg ...
- python之路之网络基础
c类地址
- ASP.NET + MVC5 入门完整教程八 -—-- 一个完整的应用程序(下)
https://blog.csdn.net/qq_21419015/article/details/80802931 SportsStore 1.导航 添加导航控件 如果客户能够通过产品列表进行分类导 ...
- MySQL5.7的参数优化
https://www.cnblogs.com/zhjh256/p/9260636.html query_cache_size = 0query_cache_type=0innodb_undo_tab ...
- LED Decorative Light Supplier Introduction - LED Track Light Products
LED Decorative Light Supplier introduction: LED track light is a track light with LED as the ligh ...
- Linux上后台运行node和springboot服务
环境:Ubuntu18.04 阿里云云服务器 尝试全局安装forever和pm2均失败,最后以linux自带的nohub启动,以前同样用nohub启动springboot 命令: nohup npm ...
- SDP协议介绍
一.SDP协议介绍 SDP 完全是一种会话描述格式 ― 它不属于传输协议 ― 它只使用不同的适当的传输协议,包括会话通知协议(SAP).会话初始协议(SIP).实时流协议(RTSP).MIME 扩展协 ...
- hdu 1045 Fire Net(二分图)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意为给定一个最大为4*4的棋盘,棋盘可以放置堡垒,处在同一行或者同一列的堡垒可以相互攻击, ...
- 牛客腾讯2020校园招聘-后台&综合-第一次笔试
第一题 Q: 小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为m|S,例如字符串 ...