HDOJ 4499 Cannon,hdoj4499cannon

來源:互聯網
上載者:User

HDOJ 4499 Cannon,hdoj4499cannon

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4499


oder?如何參加?
CannonTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 513    Accepted Submission(s): 297


Problem DescriptionIn 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. 
InputThere 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. 
OutputThere 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
 
Source2013 ACM-ICPC吉林通化全國邀請賽——題目重現

題意:給定一個棋盤,以及棋子的位置,問在該棋盤上最多還能放多少個棋子"炮",並且要保證"炮"之間不能相互攻擊。

題解:可以用搜尋做,從左至右,從上至下擺棋子,每次都要判斷該點是否可以擺放。

——開始一直WA/TLE  鬱悶——又稀裡糊塗的過了~~ 果斷時間再來看看。

AC代碼:

#include<iostream>#include<cstring>#define N 6using namespace std;int init[N][N];int n,m,q,tx,ty,res;bool check(int x,int y){    if(init[x][y])return false;   init[x][y]=2;    for(int i=0;i<y-1;i++){        if(init[x][i]==2){            int k=i+1,flag=0;            while(k<=y){                if(init[x][k]==2&&flag){                        init[x][y]=0;return false;                }                if(init[x][k]>0)flag++;                if(flag>1)break;                k++;            }        }    }    for(int i=0;i<x-1;i++){        if(init[i][y]==2){            int k=i+1,flag=0;            while(k<=x){                if(init[k][y]==2&&flag){                    init[x][y]=0;return false;                }                if(init[k][y]>0)flag++;               if(flag>1)break;                k++;            }        }    }    init[x][y]=0;    return true;}void dfs(int x,int y,int cnt){    if(cnt>res)res=cnt;     if(y==m){        x++;y=0;        if(x>=n)return;    }    for(int i=x;i<n;i++)    for(int j=y;j<m;j++){        if(check(i,j)){            init[i][j]=2;            dfs(i,j+1,cnt+1);            init[i][j]=0;        }        return dfs(i,j+1,cnt);    }}int main(){    cin.sync_with_stdio(false);    while(cin>>n>>m>>q)    {        res=0;        memset(init,0,sizeof(init));        while(q--){            cin>>tx>>ty; init[tx][ty]=1;        }        dfs(0,0,0);        cout<<res<<endl;    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.