#include <string>
#include <vector>
#include <iostream>
using namespace std;
void RotateKey(vector<vector<int>>& key)
{
int m = key.size();
vector<vector<int>> rotatedKey;
for(int c=0; c<m; c++)
{
vector<int> line;
for(int r=m-1; r>=0; r--)
{
line.push_back(key[r][c]);
}
rotatedKey.push_back(line);
}
key = rotatedKey;
}
// 자물쇠를 확장한다.
void ExpandLock(vector<vector<int>>& lock, int keySize)
{
int n = lock.size();
int exN = n + (keySize-1) * 2;
vector<vector<int>> exLock(exN, vector<int>(exN,0));
for(int r=0; r<n; r++)
{
for(int c=0; c<n; c++)
{
exLock[keySize-1+r][keySize-1+c] = lock[r][c];
}
}
lock = exLock;
}
void PutKeyOnLock(vector<vector<int>>& key, vector<vector<int>>& lock, int x, int y)
{
int m = key.size();
for(int r=0; r<m; r++)
{
for(int c=0; c<m; c++)
{
lock[x+r][y+c] += key[r][c];
}
}
}
bool IsOpened(vector<vector<int>>& lock, int n, int m)
{
for(int r=0; r<n; r++)
{
for(int c=0; c<n; c++)
{
if(lock[r+m-1][c+m-1] != 1) return false;
}
}
return true;
}
bool IsKeyOpenLock(vector<vector<int>>& key, vector<vector<int>>& lock, int n)
{
vector<vector<int>> tmpLock;
int limit = lock.size() - (key.size() - 1);
for(int r=0; r<limit; r++)
{
for(int c=0; c<limit; c++)
{
tmpLock = lock;
PutKeyOnLock(key, tmpLock, r, c);
if(IsOpened(tmpLock, n, key.size()) == true) return true;
}
}
return false;
}
bool solution(vector<vector<int>> key, vector<vector<int>> lock) {
int n = lock.size();
ExpandLock(lock, key.size());
for(int i=0; i<4; i++)
{
if(IsKeyOpenLock(key, lock, n)==true) return true;
RotateKey(key);
}
return false;
}
Leave a comment