码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5386 棋盘涂色模拟

时间:2015-08-14 11:44:58      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=5386

Problem Description
You have an n?n matrix.Every grid has a color.Now there are two types of operating:
L x y: for(int i=1;i<=n;i++)color[i][x]=y;
H x y:for(int i=1;i<=n;i++)color[x][i]=y;
Now give you the initial matrix and the goal matrix.There are m operatings.Put in order to arrange operatings,so that the initial matrix will be the goal matrix after doing these operatings

It‘s guaranteed that there exists solution.
 

Input
There are multiple test cases,first line has an integer T
For each case:
First line has two integer n,m
Then n lines,every line has n integers,describe the initial matrix
Then n lines,every line has n integers,describe the goal matrix
Then m lines,every line describe an operating

1color[i][j]n
T=5
1n100
1m500
 

Output
For each case,print a line include m integers.The i-th integer x show that the rank of x-th operating is i
 

Sample Input
1 3 5 2 2 1 2 3 3 2 1 3 3 3 3 3 3 3 3 3 3 H 2 3 L 2 2 H 3 3 H 1 3 L 2 3
 

Sample Output
5 2 4 3 1

/**
hdu5386  模拟
题目大意:给一个棋盘涂色,每次操作给棋盘的某一列或一行涂上一种颜色,给定初始棋盘状态和最终棋盘状态,和一系列操作,请给这些序列排一个顺序
解题思路:初始棋盘其实并没有什么用,我们从最终棋盘入手,每次找到一行或一列颜色全部一样的,如给定操作中有给这一行的如此操作,那么给此操作入队,
          依次下去得到一个顺序,输出即可           
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=105;

vector<pair<int,int> > x_vec[maxn],y_vec[maxn];
int a[maxn][maxn],n,m;
int flag[maxn],num[maxn*5],judge[maxn*5];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int mm=m;
        int x;
        for(int i=0;i<n*n;i++)scanf("%d",&x);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=0;i<=n;i++)
        {
            x_vec[i].clear();
            y_vec[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            char s[10];
            int x,y;
            scanf("%s%d%d",s,&x,&y);
            if(s[0]=='H')
            {
                x_vec[x].push_back(make_pair(y,i));
            }
            else
            {
                y_vec[x].push_back(make_pair(y,i));
            }
        }
        memset(judge,0,sizeof(judge));
        while(m)
        {
          //  printf("**************%d\n",m);
            int sum=0,ok=0;
            for(int i=1;i<=n&&sum==0;i++)
            {
                for(int j=1;j<=n&&sum==0;j++)
                {
                    if(a[i][j])sum++;
                    //printf("%d ",a[i][j]);
                }
              //  printf("\n");
            }
            if(sum==0)break;
            for(int i=1;i<=n;i++)
            {
                int sum=0,cnt=0;
                memset(flag,0,sizeof(flag));
                for(int j=1;j<=n;j++)
                {
                    if(a[i][j]==0)continue;
                    if(!flag[a[i][j]])
                    {
                        flag[a[i][j]]=1;
                        sum++;
                        if(sum>1)break;
                        cnt=a[i][j];
                    }
                }
                if(sum==1)
                {
                    int l1=x_vec[i].size();
                    for(int j=0;j<l1;j++)
                    {
                         if(x_vec[i][j].first==cnt)
                         {
                             ok=1;
                             num[m--]=x_vec[i][j].second;
                             judge[x_vec[i][j].second]=1;
                             break;
                         }
                    }
                    for(int j=1;j<=n;j++)
                        a[i][j]=0;
                }
            }
            for(int i=1;i<=n;i++)
            {
                int sum=0,cnt=0;
                memset(flag,0,sizeof(flag));
                for(int j=1;j<=n;j++)
                {
                    if(a[j][i]==0)continue;
                    if(!flag[a[j][i]])
                    {
                        flag[a[j][i]]=1;
                        sum++;
                        if(sum>1)break;
                        cnt=a[j][i];
                    }
                }
                if(sum==1)
                {
                    int l1=y_vec[i].size();
                    for(int j=0;j<l1;j++)
                    {
                        if(y_vec[i][j].first==cnt)
                        {
                            ok=1;
                            num[m--]=y_vec[i][j].second;
                            judge[y_vec[i][j].second]=1;
                            break;
                        }
                    }
                    for(int j=1;j<=n;j++)
                    {
                        a[j][i]=0;
                    }
                }
            }
            if(ok==0)break;///找不到要退出
        }
        for(int i=1;i<=n;i++)
        {
            int l1=x_vec[i].size();
            for(int j=0;j<l1;j++)
            {
                if(judge[x_vec[i][j].second]==0)num[m--]=x_vec[i][j].second;
            }
            l1=y_vec[i].size();
            for(int j=0;j<l1;j++)
            {
                if(judge[y_vec[i][j].second]==0)num[m--]=y_vec[i][j].second;
            }
        }
       /// printf("##################\n");
        for(int i=1;i<=mm;i++)
        {
            printf(i==mm?"%d\n":"%d ",num[i]);
        }
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu5386 棋盘涂色模拟

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/47658785

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!