[Programmers] 자물쇠와 열쇠

Time Lapse :43min 53sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <string>
#include <vector>
#include <iostream>
using namespace std;
bool func(vector<vector<int>> lock)
{
for(int i=lock.size()/3;i<lock.size()*2/3;i++)
{
for(int j=lock.size()/3;j<lock.size()*2/3;j++)
if(lock[i][j]!=1)
return false;
}
return true;
}
bool isPossible(vector<vector<int>> key,vector<vector<int>> lock)
{
for(int i=lock.size()/3-key.size()+1;i<lock.size()-key.size();i++)
{
for(int j=lock.size()/3-key.size()+1;j<lock.size()-key.size();j++)
{
vector<vector<int>> lock_cpy = lock;
for(int k=0;k<key.size();k++){
for(int f=0;f<key.size();f++){
lock_cpy[i+k][j+f]+=key[k][f];}}
if(func(lock_cpy))
return true;
}
}
return false;
}
vector<vector<int>> makeKey(vector<vector<int>> key)
{
vector<vector<int>> new_key;
for(int i=0;i<key.size();i++)
{
new_key.push_back(vector<int>());
for(int j=key.size()-1;j>=0;j--)
{
new_key[i].push_back(key[j].at(i));
}
}
return new_key;
}

bool solution(vector<vector<int>> key, vector<vector<int>> lock) {
bool answer = false;
vector<vector<int>> key2,key3,key4,new_lock;
key2=makeKey(key);
key3=makeKey(key2);
key4=makeKey(key3);
for(int i=0;i<lock.size()*3;i++)
{
new_lock.push_back(vector<int>());
for(int j=0;j<lock.size()*3;j++)
{
new_lock[i].push_back(0);
}
}

for(int i=0;i<lock.size();i++)
{
for(int j=0;j<lock.size();j++)
{
new_lock[lock.size()+i][lock.size()+j]=lock[i][j];
}
}
if(isPossible(key,new_lock)||isPossible(key2,new_lock)||isPossible(key3,new_lock)||isPossible(key4,new_lock))
return true;

return false;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/lockerAndKey/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.