POJ 1021--2D-Nim

题意

题目前面啰嗦一堆的其实是讲游戏玩法,背景介绍而已,没多大用。真实题意是判断两个游戏板上由棋子构成的图形是否可以通过变换方向和平移来相互转换。

分析

一般能想到的方法主要有两种。
第一种是十字步法,若两个图相等,那么图中任一点到左右上下的步数必然能和另一张图匹配。
第二种是求所有点间的距离,两张相等的图,那么它们所有点两两之间的距离和必然相等。

其实仔细考虑两种方法的条件,都只是必要条件而不充分,虽然已经足够解题用。(希望知道严谨证明过程的朋友留言,不胜感激)

我采用的是第二种方法。
代码如下:
Memory: 248K Time: 0MS Length:100Lines

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int width, height, points;
struct Point { int x, y; Point(int a, int b) { x = a; y = b; } };
vector<vector<Point> > Graph1;
vector<vector<Point> > Graph2;
int* matrix1, *matrix2;
void SearchGraph(vector<Point>& vecPoint, int x, int y, int* matrix)   //搜寻一个图的所有点
{
    vecPoint.push_back(Point(x, y));
    matrix[x * height + y] = 0;
    if (x > 0 && matrix[(x - 1) * height + y]) SearchGraph(vecPoint, x - 1, y, matrix);
    if (x < width - 1 && matrix[(x + 1) * height + y]) SearchGraph(vecPoint, x + 1, y, matrix);
    if (y > 0 && matrix[x * height + y - 1]) SearchGraph(vecPoint, x, y - 1, matrix);
    if (y < height - 1 && matrix[x * height + y + 1]) SearchGraph(vecPoint, x, y + 1, matrix);
}
double Calc(vector<Point>& vecPoint)      //计算点的距离和
{
    double rs = 0.0;
    for (int i = 0; i < vecPoint.size(); ++i)
        for (int j = i + 1; j < vecPoint.size(); ++j)
            rs += sqrt(double((vecPoint[i].x - vecPoint[j].x) * (vecPoint[i].x - vecPoint[j].x) + (vecPoint[i].y - vecPoint[j].y) * (vecPoint[i].y - vecPoint[j].y)));
    return rs;
}
int main()
{
    int cases = 0;
    cin >> cases;
    while (cases--)
    {
        cin >> width >> height >> points;
        matrix1 = new int[width * height];
        matrix2 = new int[width * height];
        memset(matrix1, 0, sizeof(int) * width * height);
        memset(matrix2, 0, sizeof(int) * width * height);
        int count = 0;
        int x, y;        //用一维数组来表示所有图
        while (count++ < points)
        {
            cin >> x >> y;
            matrix1[x * height + y] = 1;
        }
        while (--count)
        {
            cin >> x >> y;
            matrix2[x * height + y] = 1;
        }
        vector<Point> vecPoint;      //由点集构成的图
        for (int w = 0; w < width; ++w)
        {
            for (int h = 0; h < height; ++h)
            {
                if (matrix1[w * height + h])
                {
                    SearchGraph(vecPoint, w, h, matrix1);
                    Graph1.push_back(vecPoint);    //放入图集1
                    vecPoint.clear();
                }
                if (matrix2[w * height + h])
                {
                    SearchGraph(vecPoint, w, h, matrix2);
                    Graph2.push_back(vecPoint);    //放入图集2
                    vecPoint.clear();
                }
            }
        }
        if (Graph1.size() != Graph2.size())         cout << "NO" << endl;
        else
        {
            vector<float> vec1;
            vector<float> vec2;
            for (int i = Graph1.size() - 1; i >= 0; --i)
            {
                vec1.push_back(Calc(Graph1[i]));
                vec2.push_back(Calc(Graph2[i]));
            }
            vector<float>::iterator it;
            for (it = vec1.begin(); it != vec1.end(); ++it)
            {
                vector<float>::iterator it2 = find(vec2.begin(), vec2.end(), *it);
                if (it2 == vec2.end())
                {
                    cout << "NO" << endl;
                    break;
                }
                else    vec2.erase(it2); //若有匹配的距离和就从容器中删除
            }
            if (it == vec1.end())
                cout << "YES" << endl;
        }
        delete[] matrix2;
        delete[] matrix1;
        Graph1.clear();
        Graph2.clear();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值