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

回溯啊

Posted by Jungle_Crosser at 2009-03-17 19:43:33 on Problem 1321
#include<stdio.h>
#include<stdlib.h>
char chess[10][10];
int row[10],col[10];
int n,k,count=0;
void Back(int,int,int);
int main(void)
{
  int i;
  while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1)
  {
    for(i=0;i<n;i++)
    {
      scanf("%s",chess[i]);
    }
    Back(1,0,0);
    printf("%d\n",count);
    count=0;
  }
  system("PAUSE");
  return 0;
}
void Back(int w,int a,int b)
{
  if(w>k)
  {
    count++;
  }
  else
  {
    int i,j;
    for(i=a;i<n;i++)
    {
      for(j=b;j<n;j++)
      {
        if(chess[i][j]=='#'&&row[i]==0&&col[j]==0)
        {
          chess[i][j]='.';
          row[i]=1;
          col[j]=1;
          Back(w+1,i,j);
          chess[i][j]='#';
          row[i]=0;
          col[j]=0;
        }
      }
      b=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