ZOJ 1002
2015-07-22 11:21
288 查看
> http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 > 原题地址 #include <iostream> #include <algorithm> #include <cstdio> #include <queue> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <stack> const int MAX = 10; const int INF = 1000000000; using namespace std; int n, num, ans; char maze[MAX][MAX]; bool legal(int row, int col); (判断是否可以放置碉堡) void dfs(int k, int num); int main() { int i; while(scanf("%d", &n), n!=0) { ans=0; for(i=0; i<n; i++)scanf("%s", &maze[i]); num=0; dfs(0,num); printf("%d\n", ans); } return 0; } void dfs(int k, int num) { if(k==n*n) (用k来代替i, j. 方便回溯) { if(num>ans)ans=num; (更新最大值) return ; } else { int i = k/n; (行) int j = k%n; (列) if(maze[i][j]=='.'&&legal(i,j)) { maze[i][j]='@'; (标记当前位置) dfs(k+1,num+1); maze[i][j]='.'; (回溯) } dfs(k+1,num); (较难理解:用于统计当前位置不放置碉堡的可能的数量) } } bool legal(int row, int col) { int i; for(i=row-1; i>=0; i--) (当前列的上方是否可以放置) { if(maze[i][col]=='X')break; (如果有墙,跳出) else if(maze[i][col]=='@')return false; (如果同一列有炮台,当前位 置不可放置) } for(i=row+1; i<n; i++) (下方) { if(maze[i][col]=='X')break; else if(maze[i][col]=='@')return false; } for(i=col-1; i>=0; i--) (左方) { if(maze[row][i]=='X')break; else if(maze[row][i]=='@')return false; } for(i=col+1; i<n; i++) (右方) { if(maze[row][i]=='X')break; else if(maze[row][i]=='@')return false; } return true; }
相关文章推荐
- 控制用户创建课程权限
- SQL Server性能影响的重要结论
- Slope one简单的协同过滤算法
- Java远程调用RMI(Remote Method Invocation)
- hdoj 2473 Junk-Mail Filter 【并查集的删除 】
- Ruby中的chop和chomp
- TCP和UDP的“保护消息边界”(粘包、半包)
- HDU 1020 - Encoding
- 培训管理模块
- 菜鸟vimer成长记——第2.1章、normal模式
- SQL 同一个表中 根据一列更新另一列(不同行)
- 内存泄露
- 用GruntJS合并、压缩JS文件
- 从一个activity返回
- Slope One简单高效的协同过滤算法
- Tomcat 6.x Perm区内存泄露问题
- C++常用术语及其英文翻译的含义和简单用途总结(三)
- codevs1050
- HDU 5289 Assignment (二分+RMQ) 2015多校训练一 1002
- LocalThread分析