#1094 Lost in the City

#1094 Lost in the City
Lv.1 枚举

1级的题目,没什么好说的,直接暴力枚举。
熟悉了一下std::vector的用法,C++真的是一种比较底层的语言,用惯了C#里面的Point和OpenCV的Point,再来用STL写C++的时候特别不习惯。好在STL里有std::pair。STL相当之严谨,有空研究下。

AC代码如下:

/*
#1094 Lost in the City
#level 1
#2015.6.24
#kenny the kupa keeper
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void find_loc(int N, int M, char map[][200], char grid[3][3], vector<pair<int, int>> &loc)
{
    int i = 0, j = 0, col, row;
    bool same, result = false;
    for (row = 0; row < M - 2; row++)
    {
        for (col = 0; col < N - 2; col++)
        {
            same = true;
            for (i = 0; i < 3 && same; i++)
            {
                for (j = 0; j < 3 && same; j++)
                {
                    if (map[col + i][row + j] != grid[i][j]) same = false;
                }
            }
            if (same) loc.push_back(make_pair(col + 2, row + 2));
        }
    }
}

void rotate(char grid[3][3])
{
    char temp[3][3];
    int i, j;
    for (i = 0; i < 3; i++)//将矩阵转置
    {
        for (j = 0; j < 3; j++) temp[i][j] = grid[j][i];
    }
    for (i = 0; i < 3; i++)//交换水平翻转,在这里就是交换第一行和第三行。结合上一个循环,效果就是将3x3矩阵旋转90°
    {
        for (j = 0; j < 3; j++) grid[i][j] = temp[2 - i][j];
    }
}

int main(void)
{
    int N, M, i;
    char map[1000][200], hi[3][3];//其实可以用string的
    vector<pair<int, int>> loc;
    vector<pair<int, int>>::iterator iter;
    while (cin >> N >> M)
    {
        for (i = 0; i < N; i++) cin >> map[i];
        for (i = 0; i < 3; i++) cin >> hi[i];
        for (i = 0; i < 4; i++)
        {
            find_loc(N, M, map, hi, loc);
            rotate(hi);
        }

        sort(loc.begin(), loc.end());//排序
        int cnt = unique(loc.begin(), loc.end()) - loc.begin();//去重复(实际上是把重复的元素移动到vector后面)

        for (i = 0; i < cnt; i++) cout << loc[i].first << " " << loc[i].second << endl;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值