Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪里有数据来解救劳苦的代码呀?wa了半天,附代码。

Posted by AllwaysDead at 2008-09-30 14:04:13 on Problem 1027
用了一下并查集
#include <cstdio>
#include <string.h>
using namespace std;
int father[2250],count[2250];
int findfather(int a){return father[a]==-1?a:father[a]=findfather(father[a]);}
void together(int a,int b){int i,j;if ((i=findfather(a))!=(j=findfather(b))){
     if (i%15<j%15 || (i%15==j%15 && i/15>j/15)){father[j]=i;count[i]+=count[j];}
     else {father[i]=j;count[j]+=count[i];}
}}
char map[150][15];
int score,ball,H,pX,pY;
int workout(int &id)
{
    int i,j,maxid;
    memset(father,0xff,sizeof(father));
    for (i=0;i<H*15;i++)count[i]=1;
    for (i=0;i<H;i++)for (j=0;j<15;j++)if (map[i][j]){
        if (i>0 && map[i-1][j]==map[i][j])together(i*15+j,i*15+j-15);
        if (j>0 && map[i][j-1]==map[i][j])together(i*15+j,i*15+j-1);
        if (j+1<15 && map[i][j+1]==map[i][j])together(i*15+j,i*15+j+1);
        if (i+1<H && map[i+1][j]==map[i][j])together(i*15+j,i*15+j+15);
    }
    for (maxid=i=0;i<H*15;i++)if (father[i]==-1 && map[i/15][i%15])
        if (count[i]>count[maxid] || (count[i]==count[maxid] && (i%15<maxid%15 || (i%15==maxid%15 && i/15>maxid/15))))maxid=i;
    if (count[maxid]<=1)return 0;
    ball-=count[maxid];score+=(count[maxid]-2)*(count[maxid]-2);
    pX=maxid/15;pY=maxid%15;
    printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",
                 id, H-pX,pY+1,     count[maxid],     map[pX][pY],(count[maxid]-2)*(count[maxid]-2));
    return ++id;
}
int nextmap()
{
    int i,k,l,p,q;
    char j;
    q=findfather(pX*15+pY);
    for (i=0;i<H*15;i++)if (findfather(i)==q)map[i/15][i%15]=0;
    for (i=0;i<15;i++)for (k=l=H-1;k>=0;k--) if (map[k][i]){
                          j=map[k][i];map[k][i]=map[l][i];map[l][i]=j;
                          l--;
    }
    for (i=k=0;i<15;i++){
        for (l=0;l<H && map[l][i]==0;l++);
        if (l!=H){
           for (p=0;p<H;p++){
               j=map[p][i];map[p][i]=map[p][k];map[p][k]=j;
           }
           k++;
        }
    }
    /*for (k=0;k<H;k++){for (l=0;l<15;l++)
        printf("%c",(map[k][l]==0?' ':map[k][l]));printf("\n");
    }*/
    return 0;
}
int main()
{
    int TT,T,i,j;
    char chs[500];
    scanf("%d",&T);TT=T;
    gets(chs);
    while (T--){
          printf("Game %d:\n\n",TT-T);
          score=0;H=10;
          for (i=0;i<H;i++){gets(chs);
              for (j=0;j<15;j++)map[i][j]=chs[j];
          }
          if (T)gets(chs);
          i=1;
          for (ball=H*15;workout(i);nextmap());
          if (ball==0)score+=1000;
          printf("Final score: %d, with %d balls remaining.\n\n",score,ball);
    }
    getchar();getchar();getchar();
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator