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

简单DFS就过去了。63ms,附代码

Posted by CCSGTC at 2018-08-02 17:05:02 on Problem 1691
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdlib>
using namespace std;
#define N 100
struct Node
{
    int l_x ,l_y;
    int r_x , r_y;
    int num;
};
int Mark[30], n,minn;
Node Gra[20];
int check(int  pos) //判断pos上面的图形是否已经都被染色了
{

  for(int i = 0 ;i < n ;i++ )
  {

     if( Gra[i].r_x == Gra[pos].l_x && Gra[i].l_y <= Gra[pos].r_y  )
     {
        if(!Mark[i])
            return 0;

     }
  }

    return  1;

}
void DFS(int Color, int cnt,int ans)
{
    if(cnt == n )
    {
        if(ans <= minn)
            minn = ans;
    
        return ;
    }

     for( int i = 0 ;i < n ; i++)
     {
         if( !Mark[i] && check(i) )
         {
             if( Gra[i].num == Color )
             {
                Mark[i] = 1;
                DFS( Color, cnt + 1 ,ans );
                Mark[i] = 0;
             }
             else if( Gra[i].num != Color )
             {
                Mark[i] = 1;
                DFS(Gra[i].num, cnt + 1, ans + 1);
                Mark[i] = 0;
             }

         }
     }

     return ;
}
int  main()
{
    int cnt ;
    cin >> cnt;

    while( cnt-- )
    {
        cin >> n;
      
        memset(Mark,0,sizeof(Mark));
        for(int i = 0 ;i < n ;i++) 
           cin >> Gra[i].l_x >> Gra[i].l_y >> Gra[i].r_x >> Gra[i].r_y >> Gra[i].num;
          

        minn = 999999;
        DFS(-1,0,0);
        cout << minn << endl;




    }
    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