博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4499 Cannon (搜索)
阅读量:7051 次
发布时间:2019-06-28

本文共 3220 字,大约阅读时间需要 10 分钟。

Cannon

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 21    Accepted Submission(s): 14

Problem Description
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem. 
An eat action, for example, Cannon A eating chessman B, requires two conditions: 
1、A and B is in either the same row or the same column in the chess grid. 
2、There is exactly one chessman between A and B. 
Here comes the problem. 
Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.
 

 

Input
There are multiple test cases. 
In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen. 
In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.
 

 

Output
There is only one line for each test case, containing the maximum number of cannons.
 

 

Sample Input
4 4 2 1 1 1 2 5 5 8 0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0
 

 

Sample Output
8 9
 

 

Source
 

 

Recommend
liuyiding
 

 

 

 

 

数据范围很小,明显是搜索。

主要剪枝,就是不要和前面的冲突了、

 

1 /* *********************************************** 2 Author        :kuangbin 3 Created Time  :2013/8/24 14:38:00 4 File Name     :F:\2013ACM练习\比赛练习\2013通化邀请赛\1007.cpp 5 ************************************************ */ 6  7 #include 
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 using namespace std;20 int n,m;21 int g[10][10];22 int ans ;23 24 void dfs(int x,int y,int cnt)25 {26 if(x >= n)27 {28 ans = max(ans,cnt);29 return;30 }31 if(y >= m)32 {33 dfs(x+1,0,cnt);34 return;35 }36 if(g[x][y] == 1)37 {38 dfs(x,y+1,cnt);39 return;40 }41 dfs(x,y+1,cnt);42 bool flag = true;43 int t;44 for(t = x-1;t >= 0;t--)45 if(g[t][y])46 {47 break;48 }49 for(int i = t-1;i >= 0;i--)50 if(g[i][y])51 {52 if(g[i][y]==2)flag = false;53 break;54 }55 if(!flag)return;56 for(t = y-1;t >= 0;t--)57 if(g[x][t])58 break;59 for(int j = t-1;j >= 0;j--)60 if(g[x][j])61 {62 if(g[x][j] == 2)flag = false;63 break;64 }65 if(!flag)return;66 g[x][y] = 2;67 dfs(x,y+1,cnt+1);68 g[x][y] = 0;69 }70 71 72 int main()73 {74 //freopen("in.txt","r",stdin);75 //freopen("out.txt","w",stdout);76 int Q;77 int u,v;78 while(scanf("%d%d%d",&n,&m,&Q) == 3)79 {80 memset(g,0,sizeof(g));81 while(Q--)82 {83 scanf("%d%d",&u,&v);84 g[u][v] = 1;85 }86 ans = 0;87 dfs(0,0,0);88 printf("%d\n",ans);89 }90 return 0;91 }

 

 

 

 

 

 

 

 

转载地址:http://xqsol.baihongyu.com/

你可能感兴趣的文章
python3简单使用requests
查看>>
Web项目中创建简单的错误处理页面
查看>>
有滚动条、固定Header的ASP.Net DataGrid实现
查看>>
由一次java作业 引起的思考
查看>>
HDU 3389 Game(博弈)
查看>>
仅IE支持clearAttributes/mergeAttributes方法
查看>>
Linux中U盘和SD卡加载卸载命令
查看>>
github push403错误的处理
查看>>
Hibernate与 MyBatis的比较
查看>>
把xampp里面的apache换成nginx
查看>>
[转]基于C#的Socket简单通讯
查看>>
x264 FFmpeg Options Guide
查看>>
关于百度地图API的地图坐标转换问题
查看>>
【操作系统】设备管理(五)
查看>>
ArcObject开发时,axtoolbarcontrol中一些添加的按钮是灰色的问题
查看>>
[LeetCode] Guess Number Higher or Lower 猜数字大小
查看>>
netbeans 快捷键
查看>>
C#实现GDI+基本图的缩放、拖拽、移动
查看>>
github-ssh
查看>>
FiddlerScript学习一:改动Request或Response
查看>>