您的位置:首页 > 编程语言 > C语言/C++

#1094 Lost in the City

2015-06-24 20:27 615 查看
#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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ hihocoder