Problem B. X Squared Google Kickstart Round C 2017

题意:给定一个由X和.组成的正方形。问能否通过行行互换,列列互换得到一个大X。

small input当年用的bfs,但最后发现题目只是问能不能得到a large X shape,并不是问最优解(最少交换多少次可以到的目标状态)。所以明显是个观察找规律的题目==

当两行i,j交换时,row i和row j对应元素(e.g.,同一列的mp[i,0] and mp[j,0])的相对位置并没有变化。比如X与X原先同列交换后还是应该同列。

因为在大X中,row i与row N-i中X应该同列。所以在初始状态中也应该同列。

先判断是否仅存在一行只有一个X,其他行有且仅有两个X。

再判断对于row i,是否存在唯一的row j,两个row的X同列。对于找到的row要标记,防止重复枚举和出现row i matches row j, and row k matches row j这种情况。

再判断每个column是否符合要求。实现时将input转置在调用处理row的函数即可。最初忘了这一步导致incorrect。反例是

.X.

XX.

XX.

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<windows.h>
using namespace std;

//Kcikstart 2017 Round C Problem B. X Squared large
const int maxn=55;
int T;
int N;
char mp[maxn][maxn];
int vis[maxn];
vector<int>vec[maxn];//save index of X of each row
int solve()
{
    int cnt0=0;//has two x
    int cnt1=0;//has one x
    for(int i=0;i<N;i++)
    {
        int tmp=0;
        for(int j=0;j<N;j++)
        {
            if(mp[i][j]=='X')
            {
                vec[i].push_back(j);
                tmp++;
            }
        }
        if(tmp==2)
        {
            cnt0++;
        }
        else if(tmp==1)
        {
            cnt1++;
        }
        else
        {
            return 0;
        }

    }
    if(cnt0!=N-1||cnt1!=1)
    {
        return 0;
    }
    for(int i=0;i<N;i++)
    {
        if(vis[i]==1)
        {
            continue;
        }
        if(vec[i].size()==1)
        {
            continue;
        }
        bool flg=false;

        for(int j=i+1;j<N;j++)
        {
            if(vis[j]==1)
            {
                continue;
            }
            if(vec[i].size()!=vec[j].size())
            {
                continue;
            }
            bool flg2=true;
            for(int k=0;k<vec[i].size();k++)
            {
                if(vec[i][k]!=vec[j][k])
                {
                    flg2=false;
                }
            }
            if(flg2==true)
            {
                flg=true;
                vis[j]=1;
                break;
            }
        }
        if(flg==false)
        {
            return 0;
        }
        vis[i]=1;
    }
    return 1;
}
int main()
{
//    freopen("input.txt","r",stdin);

    freopen("B-large-practice.in","r",stdin);
    freopen("B.txt","w",stdout);
    cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<maxn;i++)
        {
            vec[i].clear();
        }
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%s",&mp[i]);
        }
        int ans=solve();
        //transpose matrix
        for(int i=0;i<N;i++)
        {
            for(int j=i+1;j<N;j++)
            {
                int tmp=mp[i][j];
                mp[i][j]=mp[j][i];
                mp[j][i]=tmp;
            }
        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<maxn;i++)
        {
            vec[i].clear();
        }
        int ans1=solve();
        if(ans==0||ans1==0)
        {
            printf("Case #%d: IMPOSSIBLE\n",ca);
        }
        else
        {
            printf("Case #%d: POSSIBLE\n",ca);
        }
        cerr<<"finish case "<<ca<<endl;
        cerr<<N<<endl;
    }
    return 0;
}

附上小数据bfs code

#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<windows.h>
using namespace std;

//Kcikstart 2017 Round C Problem B. X Squared
const int maxn=55;
int T;
int N;
char mp[maxn][maxn];
set<string>visit;
int ans=0;
string map_to_str(char mp[maxn][maxn])//map matrix to string
{
    string ret="";
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            ret+=mp[i][j];
        }
    }
    return ret;
}
class node
{
public:
    char mp[maxn][maxn];
    int step;
    node()
    {
        memset(mp,0,sizeof(mp));
        step=0;
    }
    node(char mm[maxn][maxn],int s)
    {
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                mp[i][j]=mm[i][j];
            }
        }
        step=s;
    }
};
bool check_end(char mp[maxn][maxn])
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(j==i||j==N-1-i)
            {
                if(mp[i][j]!='X')
                {
                    return false;
                }
            }
            else
            {
                if(mp[i][j]!='.')
                    return false;
            }
        }
    }
    return true;
}
void bfs()//no need to use bfs, as they only ask you to decide whether it exists instead of fins an optimal solution ()
{
    queue<node>que=queue<node>();
    string start=map_to_str(mp);
    que.push(node(mp,0));
    visit.insert(start);
    //cout<<que.size()<<endl;
    while(!que.empty())
    {
        node tmp=que.front();
        que.pop();
        if(check_end(tmp.mp)==true)
        {
//            cout<<"true"<<endl;
            ans=tmp.step;
            break;
        }
//        cout<<"step "<<tmp.step<<endl;
        // exchange two rows
        for(int i=0;i<N;i++)
        {
            for(int j=i+1;j<N;j++)
            {
                char next[maxn][maxn];
                memset(next,0,sizeof(next));
                for(int k=0;k<N;k++)
                {
                    next[i][k]=tmp.mp[j][k];
                    next[j][k]=tmp.mp[i][k];
                }
                for(int k=0;k<N;k++)
                {
                    if(k==i||k==j) continue;
                    for(int l=0;l<N;l++)
                    {
                        next[k][l]=tmp.mp[k][l];
                    }
                }
//                cout<<"exchange row "<<i<<" "<<j<<endl;
//                for(int k=0;k<N;k++)
//                {
//                    cout<<next[k]<<endl;
//                }
                string next_str=map_to_str(next);
                //cout<<next_str<<endl;
                if(visit.find(next_str)!=visit.end())
                {
                    //cout<<"exist"<<endl;
                    continue;
                }
                node next_tmp=node(next,tmp.step+1);
                visit.insert(next_str);
                que.push(next_tmp);
            }
        }
        //exchange two columns
        for(int i=0;i<N;i++)
        {
            for(int j=i+1;j<N;j++)
            {
                char next[maxn][maxn];
                memset(next,0,sizeof(next));
                for(int k=0;k<N;k++)
                {
                    next[k][i]=tmp.mp[k][j];
                    next[k][j]=tmp.mp[k][i];
                }
                for(int k=0;k<N;k++)
                {
                    if(k==i||k==j) continue;
                    for(int l=0;l<N;l++)
                    {
                        next[l][k]=tmp.mp[l][k];
                    }
                }
                //cout<<"exchange column "<<i<<" "<<j<<endl;
//                for(int k=0;k<N;k++)
//                {
//                    cout<<next[k]<<endl;
//                }
                string next_str=map_to_str(next);
                if(visit.find(next_str)!=visit.end())
                {
                    continue;
                }
                node next_tmp=node(next,tmp.step+1);
                visit.insert(next_str);
                que.push(next_tmp);
            }
        }

    }
}
int main()
{
//    freopen("input.txt","r",stdin);

    freopen("B-small-practice.in","r",stdin);
    freopen("Bsmall.txt","w",stdout);
    cin>>T;
    for(int ca=1;ca<=T;ca++)
    {
        memset(mp,0,sizeof(mp));
        visit.clear();
        ans=-1;
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%s",&mp[i]);
//            cout<<mp[i]<<endl;
        }
        //cout<<map_to_str(mp)<<endl;
        bfs();
        if(ans==-1)
        {
            printf("Case #%d: IMPOSSIBLE\n",ca);
        }
        else
        {
            printf("Case #%d: POSSIBLE\n",ca);
        }




    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值