Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jaehyun Koo
authored and
Jaehyun Koo
committed
May 17, 2016
1 parent
75a405d
commit cd19a5f
Showing
4 changed files
with
276 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <functional> | ||
#include <numeric> | ||
#include <algorithm> | ||
#include <stack> | ||
#include <queue> | ||
#include <deque> | ||
#include <vector> | ||
#include <string> | ||
#include <bitset> | ||
#include <map> | ||
#include <set> | ||
using namespace std; | ||
typedef long long lint; | ||
typedef long double llf; | ||
typedef pair<int, int> pi; | ||
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; | ||
|
||
queue<pi> q1, q2; | ||
|
||
int n, m, ret; | ||
char str[4005][4005]; | ||
bool vis[4005][4005]; | ||
|
||
void bfs(queue<pi> &q1, queue<pi> &q2, int d){ | ||
if(q1.empty()) return; | ||
ret = d; | ||
while(!q1.empty()){ | ||
pi x = q1.front(); | ||
q1.pop(); | ||
for(int i=0; i<4; i++){ | ||
if(x.first + dx[i] < 0 || x.second + dy[i] < 0 || x.first + dx[i] >= n || x.second + dy[i] >= m){ | ||
continue; | ||
} | ||
if(vis[x.first+dx[i]][x.second+dy[i]] || str[x.first+dx[i]][x.second+dy[i]] == '.') continue; | ||
vis[x.first+dx[i]][x.second+dy[i]] = 1; | ||
if(str[x.first + dx[i]][x.second + dy[i]] != str[x.first][x.second]) q2.push(pi(x.first + dx[i], x.second + dy[i])); | ||
else q1.push(pi(x.first + dx[i], x.second + dy[i])); | ||
} | ||
} | ||
bfs(q2, q1, d+1); | ||
} | ||
|
||
int main(){ | ||
cin >> n >> m; | ||
for(int i=0; i<n; i++) cin >> str[i]; | ||
q1.push(pi(0, 0)); | ||
vis[0][0] = 1; | ||
bfs(q1, q2, 1); | ||
cout << ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <functional> | ||
#include <numeric> | ||
#include <algorithm> | ||
#include <stack> | ||
#include <queue> | ||
#include <deque> | ||
#include <vector> | ||
#include <string> | ||
#include <bitset> | ||
#include <map> | ||
#include <set> | ||
using namespace std; | ||
typedef long long lint; | ||
typedef long double llf; | ||
typedef pair<int, int> pi; | ||
|
||
char str[30][100], ret[30][100]; | ||
int r, c, dp[13][41][1<<12][2]; | ||
|
||
int f(int x, int y, int bit, int lef){ | ||
if(y == c / 2) return 0; | ||
if(x == r / 2){ | ||
if(lef) return 1e9; | ||
return dp[x][y][bit][lef] = f(0, y+1, bit, 0); | ||
} | ||
if(~dp[x][y][bit][lef]) return dp[x][y][bit][lef]; | ||
int ret = 1e9; | ||
if(str[2*x+1][2*y+1] == 'X'){ | ||
if(lef && (bit >> x) % 2 == 0){ | ||
ret = min(ret, f(x+1, y, bit, 0) + 2); | ||
} | ||
if(!lef && str[2*x+2][2*y+1] == ' ' && (bit >> x) % 2 == 0){ | ||
ret = min(ret, f(x+1, y, bit, 1)); | ||
} | ||
if(!lef && (bit >> x) % 2 == 1){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 0) + 2); | ||
} | ||
if(!lef && str[2*x+1][2*y+2] == ' ' && (bit >> x) % 2 == 0){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 0)); | ||
} | ||
} | ||
else{ | ||
if(!lef && (bit >> x) % 2 == 0){ | ||
ret = min(ret, f(x+1, y, bit, 0)); | ||
} | ||
if(lef && (bit >> x) % 2 == 1){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 0) + 4); | ||
} | ||
if(!lef && (bit >> x) % 2 == 1 && str[2*x+1][2*y+2] == ' '){ | ||
ret = min(ret, f(x+1, y, bit, 0) + 2); | ||
} | ||
if(lef && (bit >> x) % 2 == 0 && str[2*x+1][2*y+2] == ' '){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 0) + 2); | ||
} | ||
if(lef && (bit >> x) % 2 == 0 && str[2*x+2][2*y+1] == ' '){ | ||
ret = min(ret, f(x+1, y, bit, 1) + 2); | ||
} | ||
if(!lef && (bit >> x) % 2 == 0 && str[2*x+1][2*y+2] == ' ' && str[2*x+2][2*y+1] == ' '){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 1)); | ||
} | ||
if(!lef && (bit >> x) % 2 == 1 && str[2*x+2][2*y+1] == ' '){ | ||
ret = min(ret, f(x+1, y, bit ^ (1<<x), 1) + 2); | ||
} | ||
} | ||
return dp[x][y][bit][lef] = ret; | ||
} | ||
|
||
void track(int x, int y, int bit, int lef){ | ||
if(y == c / 2) return; | ||
if(x == r / 2){ | ||
track(0, y+1, bit, 0); | ||
return; | ||
} | ||
if(str[2*x+1][2*y+1] == 'X'){ | ||
if(lef && (bit >> x) % 2 == 0 && f(x, y, bit, lef) == f(x+1, y, bit, 0) + 2){ | ||
track(x+1, y, bit, 0); | ||
} | ||
else if(!lef && str[2*x+2][2*y+1] == ' ' && (bit >> x) % 2 == 0 && f(x, y, bit, lef) == f(x+1, y, bit, 1)){ | ||
str[2*x+2][2*y+1] = '.'; | ||
track(x+1, y, bit, 1); | ||
} | ||
else if(!lef && (bit >> x) % 2 == 1 && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 0) + 2){ | ||
track(x+1, y, bit ^ (1<<x), 0); | ||
} | ||
else if(!lef && str[2*x+1][2*y+2] == ' ' && (bit >> x) % 2 == 0 && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 0)){ | ||
str[2*x+1][2*y+2] = '.'; | ||
track(x+1, y, bit ^ (1<<x), 0); | ||
} | ||
else{ | ||
assert(0); | ||
} | ||
} | ||
else{ | ||
if(!lef && (bit >> x) % 2 == 0 && f(x, y, bit, lef) == f(x+1, y, bit, 0)){ | ||
track(x+1, y, bit, 0); | ||
} | ||
else if(lef && (bit >> x) % 2 == 1 && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 0) + 4){ | ||
str[2*x+1][2*y+1] = '.'; | ||
track(x+1, y, bit ^ (1<<x), 0); | ||
} | ||
else if(!lef && (bit >> x) % 2 == 1 && str[2*x+1][2*y+2] == ' ' && f(x, y, bit, lef) == f(x+1, y, bit, 0) + 2){ | ||
str[2*x+1][2*y+1] = '.'; | ||
str[2*x+1][2*y+2] = '.'; | ||
track(x+1, y, bit, 0); | ||
} | ||
else if(lef && (bit >> x) % 2 == 0 && str[2*x+1][2*y+2] == ' ' && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 0) + 2){ | ||
str[2*x+1][2*y+2] = '.'; | ||
str[2*x+1][2*y+1] = '.'; | ||
track(x+1, y, bit ^ (1<<x), 0); | ||
} | ||
else if(lef && (bit >> x) % 2 == 0 && str[2*x+2][2*y+1] == ' ' && f(x, y, bit, lef) == f(x+1, y, bit, 1) + 2){ | ||
str[2*x+2][2*y+1] = '.'; | ||
str[2*x+1][2*y+1] = '.'; | ||
track(x+1, y, bit, 1); | ||
} | ||
else if(!lef && (bit >> x) % 2 == 0 && str[2*x+1][2*y+2] == ' ' && str[2*x+2][2*y+1] == ' ' && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 1)){ | ||
str[2*x+2][2*y+1] = '.'; | ||
str[2*x+1][2*y+2] = '.'; | ||
str[2*x+1][2*y+1] = '.'; | ||
track(x+1, y, bit ^ (1<<x), 1); | ||
} | ||
else if(!lef && (bit >> x) % 2 == 1 && str[2*x+2][2*y+1] == ' ' && f(x, y, bit, lef) == f(x+1, y, bit ^ (1<<x), 1) + 2){ | ||
str[2*x+2][2*y+1] = '.'; | ||
str[2*x+1][2*y+1] = '.'; | ||
track(x+1, y, bit ^ (1<<x), 1); | ||
} | ||
else{ | ||
assert(0); | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
memset(dp, -1, sizeof(dp)); | ||
scanf("%d %d\n",&r,&c); | ||
for(int i=0; i<r; i++){ | ||
fgets(str[i], 88, stdin); | ||
} | ||
cout << f(0, 0, 0, 0) << endl; | ||
track(0, 0, 0, 0); | ||
for(int i=0; i<r; i++) cout << str[i]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <functional> | ||
#include <numeric> | ||
#include <algorithm> | ||
#include <stack> | ||
#include <queue> | ||
#include <deque> | ||
#include <vector> | ||
#include <string> | ||
#include <bitset> | ||
#include <map> | ||
#include <set> | ||
using namespace std; | ||
typedef long long lint; | ||
typedef long double llf; | ||
typedef pair<int, int> pi; | ||
#include "libmean.h" | ||
|
||
int n, arr[100]; | ||
deque<int> Q; | ||
|
||
void resolve(int a, int b, int c, int d, int e){ | ||
vector<pi> v; | ||
v.emplace_back(Meandian(a, b, c, d), e); | ||
v.emplace_back(Meandian(a, b, c, e), d); | ||
v.emplace_back(Meandian(a, b, d, e), c); | ||
v.emplace_back(Meandian(a, c, d, e), b); | ||
v.emplace_back(Meandian(b, c, d, e), a); | ||
sort(v.begin(), v.end()); | ||
int sum = 0; | ||
for(int i=1; i<4; i++){ | ||
sum += v[i].first; | ||
} | ||
sum /= 2; | ||
arr[v[2].second] = sum - v[2].first; | ||
} | ||
|
||
int main(){ | ||
n = Init(); | ||
memset(arr, -1, sizeof(arr)); | ||
for(int i=0; i<n; i++){ | ||
Q.push_back(i); | ||
} | ||
while(Q.size() >= 5){ | ||
resolve(Q[0], Q[1], Q[2], Q[3], Q[4]); | ||
vector<int> tmp; | ||
for(int i=0; i<5; i++){ | ||
if(arr[Q.front()] == -1) tmp.push_back(Q.front()); | ||
Q.pop_front(); | ||
} | ||
for(auto &i : tmp){ | ||
Q.push_back(i); | ||
} | ||
} | ||
Solution(arr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters