UsacoTraining Chapter1.3 Transformations

题目链接:http://train.usaco.org/usacoprob2?a=Hs8IwBlVlxN&S=transform

题意:判断一个图能否通过某些变换变成另一个图

           好好读题啊 choose the one with the minimum number above 我以为能多次变换,写了个dfs

          参数传递方式: 我用了vector 其实标程里struct 更方便

/*
ID: nflsjxc1
TASK: transform
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

int n;
bool ff=0;
int pool [101][11][11];
int siz;

char rd[11][11];

void rt(	vector<vector<int> >&a)
{
	vector<vector<int> >b=a;
	int r=n,c=1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			b[i][j]=a[r][c];
			r--;
			if(r==0)
			{
				r=n;
				c++;
			}
		}
	}
	a=b;
}
void ref(	vector<vector<int> >&a)
{
	vector<vector<int> >b=a;
	int r=1,c=n;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			b[i][j]=a[i][n+1-j];
		}
	}
	a=b;
}

void output(int a[11][11])
{
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cout<<a[i][j];
		}
		cout<<'\n';
	}
}

int check(	vector<vector<int> >cur,	vector<vector<int> >tar)
{
	bool f=0;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			if(cur[i][j]!=tar[i][j])
			{
				f=1;
				break;
			}
		}
	}
	if(!f)
	{
		return 1;
	}
	return 0;
}
int ouput=100;
void ans(int ro,int re)
{
	if(!ff)ouput=min(ouput,6);
	if(re&&ro)
	{
		re--;
		ro=0;
		ouput=min(ouput,5);
	}
	if(ro==1)ouput=min(ouput,1);
	if(ro==2)ouput=min(ouput,2);
	if(ro==3)ouput=min(ouput,3);
	if(re==1)ouput=min(ouput,4);
}

void dfs(vector<vector<int> >pos,vector<vector<int> > tar,int ro,int re)
{
	if(ro>3||re>1)return ;
	if(check(pos,tar)&&(re||ro))
	{
		ans(ro,re);
		return ;
	}
	vector<vector<int> >tmp=pos;
	rt(tmp);
	dfs(tmp,tar,ro+1,re);
	tmp=pos;
	ref(tmp);
	dfs(tmp,tar,ro,re+1);
}

int main()
{
	freopen("transform.in","r",stdin);
	freopen("transform.out","w",stdout);
	ios::sync_with_stdio(0);

	cin>>n;
	vector<vector<int> >a,b;
	a.resize(11);
	b.resize(11);
	for(int i=0; i<11; i++)
	{
		a[i].resize(11);
		b[i].resize(11);
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cin>>rd[i][j];
			if(rd[i][j]=='@')a[i][j]=1;
			else a[i][j]=0;
		}
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cin>>rd[i][j];
			if(rd[i][j]=='@')b[i][j]=1;
			else b[i][j]=0;
		}
	}

	
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			if(a[i][j]!=b[i][j])
			{
				ff=1;
				break;
			}
		}
	}

	dfs(a,b,0,0);
	ouput=min(ouput,7);
	cout<<ouput<<'\n';


	return 0;
}

因为看错了题的后果


标程如此简单:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXN 10

typedef struct Board Board;
struct Board {
    int n;
    char b[MAXN][MAXN];
};

/* rotate 90 degree clockwise: [r, c] -> [c, n - r] */
Board
rotate(Board b)
{
    Board nb;
    int r, c;

    nb.n = b.n;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            nb.b[c][b.n-1 - r] = b.b[r][c];
    return nb;
}

/* reflect board horizontally: [r, c] -> [r, n-1 -c] */
Board
reflect(Board b)
{
    Board nb;
    int r, c;

    nb.n = b.n;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            nb.b[r][b.n-1 - c] = b.b[r][c];
    return nb;
}

/* return non-zero if and only if boards are equal */
int
eqboard(Board b, Board bb)
{
    int r, c;

    if(b.n != bb.n)
        return 0;
    for(r=0; r<b.n; r++)
        for(c=0; c<b.n; c++)
            if(b.b[r][c] != bb.b[r][c])
                return 0;
    return 1;
}

Board
rdboard(FILE *fin, int n)
{
    Board b;
    int r, c;

    b.n = n;
    for(r=0; r<n; r++) {
        for(c=0; c<n; c++)
            b.b[r][c] = getc(fin);
        assert(getc(fin) == '\n');
    }
    return b;
}

void
main(void)
{
    FILE *fin, *fout;
    Board b, nb;
    int n, change;

    fin = fopen("transform.in", "r");
    fout = fopen("transform.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d\n", &n);
    b = rdboard(fin, n);
    nb = rdboard(fin, n);

    if(eqboard(nb, rotate(b)))
        change = 1;
    else if(eqboard(nb, rotate(rotate(b))))
        change = 2;
    else if(eqboard(nb, rotate(rotate(rotate(b)))))
        change = 3;
    else if(eqboard(nb, reflect(b)))
        change = 4;
    else if(eqboard(nb, rotate(reflect(b)))
         || eqboard(nb, rotate(rotate(reflect(b))))
         || eqboard(nb, rotate(rotate(rotate(reflect(b))))))
        change = 5;
    else if(eqboard(nb, b))
        change = 6;
    else
        change = 7;

    fprintf(fout, "%d\n", change);

    exit(0);
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值