HDOJ 1507 Uncle Tom's Inherited Land*

來源:互聯網
上載者:User

標籤:des   style   class   code   java   http   


直接對每個格子進行dfs結果除以2可以得到答案但是有大量重複的結果,不好輸出答案.

可以只對橫縱座標相加是奇數的格子dfs....

Uncle Tom‘s Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1728    Accepted Submission(s): 723
Special Judge


Problem DescriptionYour old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle‘s request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle‘s property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks). 

 
InputInput will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.
 
OutputFor each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.
 
Sample Input
4 461 11 42 24 14 24 44 344 23 22 23 10 0
 
Sample Output
4(1,2)--(1,3)(2,1)--(3,1)(2,3)--(3,3)(2,4)--(3,4)3(1,1)--(2,1)(1,2)--(1,3)(2,3)--(3,3)
 
SourceSouth America 2002 - Practice 

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <set>#include <vector>using namespace std;const int dir_x[4]={-1,1,0,0};const int dir_y[4]={0,0,-1,1};int mp[120][120];int n,m,k;bool used[120][120];int linker[120][120];bool dfs(int x,int y){    for(int i=0;i<4;i++)    {        int X=x+dir_x[i];        int Y=y+dir_y[i];        if(mp[X][Y]==1) continue;        if(X>n||X<1||Y>m||Y<1) continue;        if(used[X][Y]) continue;        used[X][Y]=true;        if(linker[X][Y]==-1||dfs(linker[X][Y]/1000,linker[X][Y]%1000))        {            linker[X][Y]=x*1000+y;            return true;        }    }    return false;}int hungary(){    int ret=0;    memset(linker,-1,sizeof(linker));    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if((i+j)&1||mp[i][j]==1) continue;            memset(used,false,sizeof(used));            if(dfs(i,j)) ret++;        }    }    return ret;}int main(){    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)    {        scanf("%d",&k);        memset(mp,0,sizeof(mp));        for(int i=0;i<k;i++)        {            int a,b;            scanf("%d%d",&a,&b);            mp[a][b]=1;        }        printf("%d\n",hungary());        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                int ii=linker[i][j]/1000; int jj=linker[i][j]%1000;                if(jj!=-1)                printf("(%d,%d)--(%d,%d)\n",i,j,ii,jj);            }        }    }    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.