buggy_code
stringlengths 11
625k
| fixed_code
stringlengths 17
625k
| bug_type
stringlengths 2
4.45k
| language
int64 0
8
| token_count
int64 5
200k
|
|---|---|---|---|---|
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <utility>
bool CheckMSPrime(int _n) {
for (int i = 2; (i * i) <= _n; i++) {
if (_n % i == 0) {
if (i % 7 == 1 || i % 7 == 6) {
return false;
}
}
}
return true;
}
void PrintPrime(int _n) {
printf("%d:", _n);
for (int i = 2; i <= (_n / 2); i++) {
if (_n % i == 0) {
if (i % 7 == 1 || i % 7 == 6) {
if (CheckMSPrime(i) == true) {
printf("%d ", i);
}
}
}
}
if (_n % 7 == 1 || _n % 7 == 6) {
if (CheckMSPrime(_n) == true) {
printf(" %d", _n);
}
}
printf("\n");
}
int main() {
int n;
while (true) {
scanf("%d", &n);
if (n == 1) {
break;
}
PrintPrime(n);
}
return 0;
}
|
#include <algorithm>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <utility>
bool CheckMSPrime(int _n) {
for (int i = 2; (i * i) <= _n; i++) {
if (_n % i == 0) {
if (i % 7 == 1 || i % 7 == 6) {
return false;
}
}
}
return true;
}
void PrintPrime(int _n) {
printf("%d:", _n);
for (int i = 2; i <= (_n / 2); i++) {
if (_n % i == 0) {
if (i % 7 == 1 || i % 7 == 6) {
if (CheckMSPrime(i) == true) {
printf(" %d", i);
}
}
}
}
if (_n % 7 == 1 || _n % 7 == 6) {
if (CheckMSPrime(_n) == true) {
printf(" %d", _n);
}
}
printf("\n");
}
int main() {
int n;
while (true) {
scanf("%d", &n);
if (n == 1) {
break;
}
PrintPrime(n);
}
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
| 1
| 239
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct { int to, cost; };
const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAXNUM = 300000;
bool isSatNum[MAXNUM + 1];
void SatNum() {
rep(i, MAXNUM + 1) isSatNum[i] = true;
for (int i = 2; i < sqrt(MAXNUM) + 1; i++) {
if (isSatNum[i] && (i % 7 == 1 || i % 7 == 6)) {
for (int j = i * 2; j < MAXNUM + 1; j += i) {
isSatNum[j] = false;
}
}
}
}
int main() {
SatNum();
vector<int> SatNums;
REP(i, 2, MAXNUM + 1)
if (isSatNum[i] && (i % 7 == 1 || i % 7 == 6)) SatNums.push_back(i);
int N;
while (cin >> N, N != 1) {
cout << N << ":";
vector<int> ans;
for (int i = 0; i < SatNums.size() && SatNums[i] <= N; i++) {
if (N % SatNums[i] == 0)
ans.push_back(SatNums[i]);
}
for (auto x : ans) {
cout << " " << x;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)
using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct { int to, cost; };
const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAXNUM = 300000;
bool isSatNum[MAXNUM + 1];
void SatNum() {
rep(i, MAXNUM + 1) isSatNum[i] = true;
for (int i = 2; i < sqrt(MAXNUM) + 1; i++) {
if (isSatNum[i] && (i % 7 == 1 || i % 7 == 6)) {
for (int j = i * 2; j < MAXNUM + 1; j += i) {
isSatNum[j] = false;
}
}
}
}
int main() {
SatNum();
vector<int> SatNums;
REP(i, 2, MAXNUM + 1)
if (isSatNum[i] && (i % 7 == 1 || i % 7 == 6)) SatNums.push_back(i);
int N;
while (cin >> N, N != 1) {
cout << N << ":";
vector<int> ans;
for (int i = 0; i < SatNums.size() && SatNums[i] <= N; i++) {
if (N % SatNums[i] == 0)
ans.push_back(SatNums[i]);
}
for (auto x : ans) {
cout << " " << x;
}
cout << endl;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
| 1
| 389
|
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 300000
int n;
bool prime[MAX];
int main() {
for (int i = 0; i < MAX; i++)
prime[i] = true;
for (int i = 6; i * i < MAX; i++)
if (i % 7 == 1 || i % 7 == 6)
if (prime[i])
for (int j = i; i * j < MAX; j++)
if (j % 7 == 1 || j % 7 == 6)
prime[i * j] = false;
while (1) {
scanf("%d", &n);
if (n == 1)
return 0;
printf("%d:", n);
for (int i = 1; i <= n; i++)
if (n % i == 0 && prime[i])
if (i % 7 == 1 || i % 7 == 6)
if (n / i % 7 == 1 || n / i % 7 == 6)
printf(" %d", i);
printf("\n");
}
}
|
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 300000
int n;
bool prime[MAX];
int main() {
for (int i = 0; i < MAX; i++)
prime[i] = true;
for (int i = 6; i * i < MAX; i++)
if (i % 7 == 1 || i % 7 == 6)
if (prime[i])
for (int j = i; i * j < MAX; j++)
if (j % 7 == 1 || j % 7 == 6)
prime[i * j] = false;
while (1) {
scanf("%d", &n);
if (n == 1)
return 0;
printf("%d:", n);
for (int i = 6; i <= n; i++)
if (n % i == 0 && prime[i])
if (i % 7 == 1 || i % 7 == 6)
if (n / i % 7 == 1 || n / i % 7 == 6)
printf(" %d", i);
printf("\n");
}
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
| 1
| 232
|
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
int main() {
int const N = 300000;
vector<int> pr(N, 1);
pr[0] = pr[1] = 0;
REP(i, N)
if (i % 7 != 1 && i % 7 != 6)
pr[i] = 0;
for (int i = 8; i < N; i += 7)
if (pr[i])
for (int j = i * 2; j < N; j += i)
pr[j] = 0;
for (int i = 6; i < N; i += 7)
if (pr[i])
for (int j = i * 2; j < N; j += i)
pr[j] = 0;
int n;
while (cin >> n, n != 1) {
printf("%d:", n);
for (int i = 2; i < n; i++) {
if (pr[i] && n % i == 0) {
cout << " " << i;
}
}
cout << endl;
}
}
|
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) \
for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
int main() {
int const N = 300001;
vector<int> pr(N, 1);
pr[0] = pr[1] = 0;
REP(i, N)
if (i % 7 != 1 && i % 7 != 6)
pr[i] = 0;
for (int i = 8; i < N; i += 7)
if (pr[i])
for (int j = i * 2; j < N; j += i)
pr[j] = 0;
for (int i = 6; i < N; i += 7)
if (pr[i])
for (int j = i * 2; j < N; j += i)
pr[j] = 0;
int n;
while (cin >> n, n != 1) {
printf("%d:", n);
for (int i = 2; i < n + 1; i++) {
if (pr[i] && n % i == 0) {
cout << " " << i;
}
}
cout << endl;
}
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
| 1
| 263
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool num[300001] = {true, true, false};
for (int i = 0; i < 300001; i++) {
if (i % 7 != 1 && i % 7 != 6) {
num[i] = true;
}
if (num[i] == false) {
for (int j = i * 2; j < 300001; j += i) {
num[j] = true;
}
}
}
int n;
while (cin >> n) {
vector<int> ans;
if (n == 1) {
break;
}
for (int i = 0; i < n; i++) {
if (!num[i]) {
if (!(n % i)) {
ans.push_back(i);
}
}
}
cout << n << ":";
for (int i = 0; i < ans.size(); i++) {
cout << " " << ans[i];
}
cout << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
bool num[300001] = {true, true, false};
for (int i = 0; i < 300001; i++) {
if (i % 7 != 1 && i % 7 != 6) {
num[i] = true;
}
if (num[i] == false) {
for (int j = i * 2; j < 300001; j += i) {
num[j] = true;
}
}
}
int n;
while (cin >> n) {
vector<int> ans;
if (n == 1) {
break;
}
for (int i = 0; i <= n; i++) {
if (!num[i]) {
if (!(n % i)) {
ans.push_back(i);
}
}
}
cout << n << ":";
for (int i = 0; i < ans.size(); i++) {
cout << " " << ans[i];
}
cout << endl;
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19]]
| 1
| 221
|
#include <iostream>
using namespace std;
#define MAX_N 300000
int pl[MAX_N];
char ip[MAX_N];
int p = 0;
main() {
int i, n = 300000;
for (i = 0; i <= n; i++)
ip[i] = 1;
ip[0] = ip[1] = 0;
for (i = 2; i <= n; i++) {
if (i % 7 != 1 && i % 7 != 6)
ip[i] = 0;
if (ip[i]) {
for (int j = 2 * i; j <= n; j += i)
ip[j] = 0;
pl[p++] = i;
}
}
while (cin >> n, n != 1) {
printf("%d:", n);
for (i = 0; i < p; i++)
if (!(n % pl[i]))
printf(" %d", pl[i]);
puts("");
}
}
|
#include <iostream>
using namespace std;
#define MAX_N 300000
int pl[MAX_N + 2];
char ip[MAX_N + 2];
int p = 0;
main() {
int i, n = 300000;
for (i = 0; i <= n; i++)
ip[i] = 1;
ip[0] = ip[1] = 0;
for (i = 2; i <= n; i++) {
if (i % 7 != 1 && i % 7 != 6)
ip[i] = 0;
if (ip[i]) {
for (int j = 2 * i; j <= n; j += i)
ip[j] = 0;
pl[p++] = i;
}
}
while (cin >> n, n != 1) {
printf("%d:", n);
for (i = 0; i < p; i++)
if (!(n % pl[i]))
printf(" %d", pl[i]);
puts("");
}
}
|
[["+", 0, 30, 0, 43, 49, 80, 81, 16, 17, 72], ["+", 0, 30, 0, 43, 49, 80, 81, 16, 12, 13]]
| 1
| 211
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 300001
typedef long long ll;
int main() {
bool p[MAX];
vector<ll> ms;
int n;
memset(p, true, sizeof(p));
for (int i = 1; i < MAX; i++) {
if (i % 7 == 1 || i % 7 == 6)
ms.push_back(i);
}
for (ll i = 1; ms[i] * ms[i] < MAX; i++) {
if (p[ms[i]]) {
for (ll j = ms[i] * ms[i]; j < MAX; j += ms[i]) {
p[j] = 0;
}
}
}
vector<ll> msp;
for (int i = 1; i < ms.size(); i++) {
if (p[ms[i]])
msp.push_back(ms[i]);
}
while (cin >> n) {
if (n == 1)
break;
printf("%d:", n);
for (int i = 1; i < msp.size(); i++) {
if (n % msp[i] == 0)
cout << ' ' << msp[i];
}
cout << endl;
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 300001
typedef long long ll;
int main() {
bool p[MAX];
vector<ll> ms;
int n;
memset(p, true, sizeof(p));
for (int i = 1; i < MAX; i++) {
if (i % 7 == 1 || i % 7 == 6)
ms.push_back(i);
}
for (ll i = 1; ms[i] * ms[i] < MAX; i++) {
if (p[ms[i]]) {
for (ll j = ms[i] * ms[i]; j < MAX; j += ms[i]) {
p[j] = false;
}
}
}
vector<ll> msp;
for (int i = 1; i < ms.size(); i++) {
if (p[ms[i]])
msp.push_back(ms[i]);
}
while (cin >> n) {
if (n == 1)
break;
printf("%d:", n);
for (int i = 0; i < msp.size(); i++) {
if (n % msp[i] == 0)
cout << ' ' << msp[i];
}
cout << endl;
}
return 0;
}
|
[["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 147], ["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
| 1
| 283
|
#include <iostream>
#include <vector>
using namespace std;
#define MAX 300001
bool check[MAX];
void print(int n) { cout << n << ':'; }
void init() { fill(check, check + MAX, false); }
int main() {
int n;
while (cin >> n, n != 1) {
vector<int> v;
init();
print(n);
for (int i = 2; i <= n; i++) {
if (n % i == 0 && (i % 7 == 1 || i % 7 == 6)) {
for (int j = 2 * i; j <= n; j += i)
check[j] = true;
if (!check[i])
v.push_back(i);
}
}
for (int i = 0; i < v.size(); i++) {
if (!i)
cout << v[i];
else
cout << ' ' << v[i];
}
cout << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
#define MAX 300001
bool check[MAX];
void print(int n) { cout << n << ": "; }
void init() { fill(check, check + MAX, false); }
int main() {
int n;
while (cin >> n, n != 1) {
vector<int> v;
init();
print(n);
for (int i = 2; i <= n; i++) {
if (n % i == 0 && (i % 7 == 1 || i % 7 == 6)) {
for (int j = 2 * i; j <= n; j += i)
check[j] = true;
if (!check[i])
v.push_back(i);
}
}
for (int i = 0; i < v.size(); i++) {
if (!i)
cout << v[i];
else
cout << ' ' << v[i];
}
cout << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 16, 12, 103, 0, 104], ["-", 8, 9, 0, 1, 0, 16, 12, 103, 0, 125], ["+", 8, 9, 0, 1, 0, 16, 12, 5, 0, 62], ["+", 8, 9, 0, 1, 0, 16, 12, 5, 0, 6]]
| 1
| 216
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<bool> VB;
VI ms_prime_factor(void) {
const int SIZE = 300010;
VB is_ms_prime_number(SIZE, false);
VI res;
for (int i = 1; 7 * i + 10 < SIZE; ++i) {
is_ms_prime_number[7 * i + 1] = true;
is_ms_prime_number[7 * i + 6] = true;
}
for (int i = 0; i < SIZE; ++i) {
if (!is_ms_prime_number[i]) {
continue;
}
for (int j = i + i; j < SIZE; j += i) {
is_ms_prime_number[j] = false;
}
res.push_back(i);
}
return res;
}
int main(void) {
VI p_factor = ms_prime_factor();
int num;
while (cin >> num, num != 1) {
cout << num << ':';
for (int i = 0; i < p_factor.size(); ++i) {
if (num % p_factor[i] == 0) {
cout << ' ' << p_factor[i];
}
}
cout << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<bool> VB;
VI ms_prime_factor(void) {
const int SIZE = 300010;
VB is_ms_prime_number(SIZE, false);
VI res;
for (int i = 0; 7 * i + 10 < SIZE; ++i) {
is_ms_prime_number[7 * i + 1] = true;
is_ms_prime_number[7 * i + 6] = true;
}
for (int i = 2; i < SIZE; ++i) {
if (!is_ms_prime_number[i]) {
continue;
}
for (int j = i + i; j < SIZE; j += i) {
is_ms_prime_number[j] = false;
}
res.push_back(i);
}
return res;
}
int main(void) {
VI p_factor = ms_prime_factor();
int num;
while (cin >> num, num != 1) {
cout << num << ':';
for (int i = 0; i < p_factor.size(); ++i) {
if (num % p_factor[i] == 0) {
cout << ' ' << p_factor[i];
}
}
cout << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13]]
| 1
| 242
|
#include <algorithm>
#include <iostream>
using namespace std;
#define H 60
#define W 30
#define INF 10000
#define R 1
#define L 0
int w, h;
char maps[H][W];
struct point {
int x, y;
};
bool invalid(int x, int y) { return (x < 0 || x >= w || y < 0 || y >= h); }
int dijkstra() {
int d[H][W][2];
bool used[H][W][2];
int ldx[9] = {-1, -1, -1, -1, -1, -2, -2, -2, -3};
int ldy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
int rdx[9] = {1, 1, 1, 1, 1, 2, 2, 2, 3};
int rdy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < 2; k++) {
d[i][j][k] = INF;
used[i][j][k] = false;
}
}
}
for (int i = 0; i < w - 1; i++) {
if (maps[h - 1][i] == 'S') {
d[h - 1][i][L] = 0;
d[h - 1][i + 1][R] = 0;
}
}
while (true) {
point p;
p.x = -1;
p.y = -1;
int muki = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < 2; k++) {
if (!used[i][j][k] && (p.x == -1 || d[i][j][k] < d[p.y][p.x][muki])) {
p.x = j;
p.y = i;
muki = k;
}
}
}
}
if (p.x == -1 && p.y == -1) {
break;
}
used[p.y][p.x][muki] = true;
if (muki == R) {
for (int i = 0; i < 9; i++) {
int mx = p.x + ldx[i];
int my = p.y + ldy[i];
if (invalid(mx, my)) {
continue;
}
if (maps[my][mx] == 'X') {
continue;
}
if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
d[my][mx][L] = min(d[my][mx][L], d[p.y][p.x][R]);
} else {
d[my][mx][L] =
min(d[my][mx][L], d[p.y][p.x][R] + (maps[my][mx] - '0'));
}
}
} else {
for (int i = 0; i < 9; i++) {
int mx = p.x + rdx[i];
int my = p.y + rdy[i];
if (invalid(mx, my)) {
continue;
}
if (maps[my][mx] == 'X') {
continue;
}
if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
d[my][mx][R] = min(d[my][mx][R], d[p.y][p.x][L]);
} else {
d[my][mx][R] =
min(d[my][mx][R], d[p.y][p.x][L] + (maps[my][mx] - '0'));
}
}
}
}
int ans = INF;
for (int j = 0; j < w; j++) {
if (maps[0][j] == 'T') {
for (int k = 0; k < 2; k++) {
ans = min(ans, d[0][j][k]);
}
}
}
if (ans == INF) {
return -1;
} else {
return ans;
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> maps[i][j];
}
}
cout << dijkstra() << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
#define H 60
#define W 30
#define INF 10000
#define R 1
#define L 0
int w, h;
char maps[H][W];
struct point {
int x, y;
};
bool invalid(int x, int y) { return (x < 0 || x >= w || y < 0 || y >= h); }
int dijkstra() {
int d[H][W][2];
bool used[H][W][2];
int ldx[9] = {-1, -1, -1, -1, -1, -2, -2, -2, -3};
int ldy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
int rdx[9] = {1, 1, 1, 1, 1, 2, 2, 2, 3};
int rdy[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < 2; k++) {
d[i][j][k] = INF;
used[i][j][k] = false;
}
}
}
for (int i = 0; i < w; i++) {
if (maps[h - 1][i] == 'S') {
d[h - 1][i][L] = 0;
d[h - 1][i][R] = 0;
}
}
while (true) {
point p;
p.x = -1;
p.y = -1;
int muki = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 0; k < 2; k++) {
if (!used[i][j][k] && (p.x == -1 || d[i][j][k] < d[p.y][p.x][muki])) {
p.x = j;
p.y = i;
muki = k;
}
}
}
}
if (p.x == -1 && p.y == -1) {
break;
}
used[p.y][p.x][muki] = true;
if (muki == R) {
for (int i = 0; i < 9; i++) {
int mx = p.x + ldx[i];
int my = p.y + ldy[i];
if (invalid(mx, my)) {
continue;
}
if (maps[my][mx] == 'X') {
continue;
}
if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
d[my][mx][L] = min(d[my][mx][L], d[p.y][p.x][R]);
} else {
d[my][mx][L] =
min(d[my][mx][L], d[p.y][p.x][R] + (maps[my][mx] - '0'));
}
}
} else {
for (int i = 0; i < 9; i++) {
int mx = p.x + rdx[i];
int my = p.y + rdy[i];
if (invalid(mx, my)) {
continue;
}
if (maps[my][mx] == 'X') {
continue;
}
if (maps[my][mx] == 'S' || maps[my][mx] == 'T') {
d[my][mx][R] = min(d[my][mx][R], d[p.y][p.x][L]);
} else {
d[my][mx][R] =
min(d[my][mx][R], d[p.y][p.x][L] + (maps[my][mx] - '0'));
}
}
}
}
int ans = INF;
for (int j = 0; j < w; j++) {
if (maps[0][j] == 'T') {
for (int k = 0; k < 2; k++) {
ans = min(ans, d[0][j][k]);
}
}
}
if (ans == INF) {
return -1;
} else {
return ans;
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> maps[i][j];
}
}
cout << dijkstra() << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 1,104
|
#include <algorithm>
#include <iostream>
#include <queue>
#define INF (1e9)
#define H 60
#define W 30
using namespace std;
typedef pair<int, int> P;
typedef pair<char, P> P1;
typedef pair<int, P1> P2;
int dijkstra(int, int, char);
int w, h, dl[H][W], dr[H][W], ans;
char s[H][W];
int main() {
while (1) {
cin >> w >> h;
if (!w && !h)
break;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> s[i][j];
ans = INF;
for (int i = 0; i < w; i++)
if (s[h - 1][i] == 'S')
ans = min(ans, min(dijkstra(h - 1, i, 'l'), dijkstra(h - 1, i, 'r')));
if (ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}
int dijkstra(int sy, int sx, char lr) {
priority_queue<P2> q;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
dl[i][j] = dr[i][j] = INF;
q.push(P2(0, P1(lr, P(sy, sx))));
if (lr == 'l')
dl[sy][sx] = 0;
else
dr[sy][sx] = 0;
while (!q.empty()) {
P2 t = q.top();
q.pop();
char c = t.second.first;
int cost = t.first, y = t.second.second.first, x = t.second.second.second;
int dy[9] = {0, 0, 0, -1, -1, -2, 1, 1, 2},
dx[9] = {1, 2, 3, 1, 2, 1, 1, 2, 1};
if (c == 'l' && dr[y][x] < cost)
continue;
if (c == 'r' && dl[y][x] < cost)
continue;
for (int i = 0; i < 9; i++) {
int ny, nx;
ny = y + dy[i];
if (c == 'l')
nx = x + dx[i];
else
nx = x - dx[i];
if (ny < 0 || nx < 0 || h <= ny || w <= nx || s[ny][nx] == 'X' ||
s[ny][nx] == 'S')
continue;
if (s[ny][nx] == 'T') {
if (c == 'l')
dr[ny][nx] = min(dr[ny][nx], cost);
else
dl[ny][nx] = min(dl[ny][nx], cost);
} else if (c == 'l' && dr[ny][nx] > cost + s[ny][nx] - '0') {
dr[ny][nx] = cost + s[ny][nx] - '0';
q.push(P2(dr[ny][nx], P1('r', P(ny, nx))));
} else if (c == 'r' && dl[ny][nx] > cost + s[ny][nx] - '0') {
dl[ny][nx] = cost + s[ny][nx] - '0';
q.push(P2(dl[ny][nx], P1('l', P(ny, nx))));
}
}
}
int res = INF;
for (int i = 0; i < w; i++)
if (s[0][i] == 'T')
res = min(res, min(dl[0][i], dr[0][i]));
return res;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#define INF (1e9)
#define H 60
#define W 30
using namespace std;
typedef pair<int, int> P;
typedef pair<char, P> P1;
typedef pair<int, P1> P2;
int dijkstra(int, int, char);
int w, h, dl[H][W], dr[H][W], ans;
char s[H][W];
int main() {
while (1) {
cin >> w >> h;
if (!w && !h)
break;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> s[i][j];
ans = INF;
for (int i = 0; i < w; i++)
if (s[h - 1][i] == 'S')
ans = min(ans, min(dijkstra(h - 1, i, 'l'), dijkstra(h - 1, i, 'r')));
if (ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}
int dijkstra(int sy, int sx, char lr) {
priority_queue<P2> q;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
dl[i][j] = dr[i][j] = INF;
q.push(P2(0, P1(lr, P(sy, sx))));
if (lr == 'l')
dl[sy][sx] = 0;
else
dr[sy][sx] = 0;
while (!q.empty()) {
P2 t = q.top();
q.pop();
char c = t.second.first;
int cost = -t.first, y = t.second.second.first, x = t.second.second.second;
int dy[9] = {0, 0, 0, -1, -1, -2, 1, 1, 2},
dx[9] = {1, 2, 3, 1, 2, 1, 1, 2, 1};
if (c == 'l' && dl[y][x] < cost)
continue;
if (c == 'r' && dr[y][x] < cost)
continue;
for (int i = 0; i < 9; i++) {
int ny, nx;
ny = y + dy[i];
if (c == 'l')
nx = x + dx[i];
else
nx = x - dx[i];
if (ny < 0 || nx < 0 || h <= ny || w <= nx || s[ny][nx] == 'X' ||
s[ny][nx] == 'S')
continue;
if (s[ny][nx] == 'T') {
if (c == 'l')
dr[ny][nx] = min(dr[ny][nx], cost);
else
dl[ny][nx] = min(dl[ny][nx], cost);
} else if (c == 'l' && dr[ny][nx] > cost + s[ny][nx] - '0') {
dr[ny][nx] = cost + s[ny][nx] - '0';
q.push(P2(-dr[ny][nx], P1('r', P(ny, nx))));
} else if (c == 'r' && dl[ny][nx] > cost + s[ny][nx] - '0') {
dl[ny][nx] = cost + s[ny][nx] - '0';
q.push(P2(-dl[ny][nx], P1('l', P(ny, nx))));
}
}
}
int res = INF;
for (int i = 0; i < w; i++)
if (s[0][i] == 'T')
res = min(res, min(dl[0][i], dr[0][i]));
return res;
}
|
[["+", 8, 9, 0, 43, 49, 50, 51, 91, 17, 33], ["-", 51, 16, 12, 16, 31, 69, 28, 69, 28, 22], ["+", 51, 16, 12, 16, 31, 69, 28, 69, 28, 22], ["+", 3, 4, 0, 2, 3, 4, 0, 91, 17, 33]]
| 1
| 916
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
{-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int dy[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};
typedef vector<vvi> vvvi;
struct node {
int foot, x, y, c;
node() {}
node(int foot, int x, int y, int c) : foot(foot), x(x), y(y), c(c) {}
};
const int MAX = INT_MAX / 10;
int main() {
int w, h;
while (cin >> w >> h) {
vvi s(w, vi(h));
vector<pii> start, goal;
REP(j, h) REP(i, w) {
char c;
cin >> c;
switch (c) {
case 'S':
start.push_back(make_pair(i, j));
s[i][j] = 0;
break;
case 'T':
goal.push_back(make_pair(i, j));
s[i][j] = 0;
break;
case 'X':
s[i][j] = -1;
break;
default:
s[i][j] = c - '0';
break;
}
}
vvvi cost(2, vvi(w, vi(h, MAX)));
queue<node> q;
REP(i, 2) REP(j, start.size()) {
q.push(node(i, start[j].first, start[j].second, 0));
cost[i][start[j].first][start[j].second] = 0;
}
while (!q.empty()) {
node n = q.front();
q.pop();
if (cost[n.foot][n.x][n.y] < n.c)
continue;
REP(i, 9) {
int foot = (n.foot + 1) % 2;
int x = n.x + dx[foot][i], y = n.y + dy[foot][i];
if (x >= 0 && y >= 0 && x < w && y < h && s[x][y] >= 0) {
int newcost = cost[n.foot][n.x][n.y] + s[x][y];
if (cost[foot][x][y] > newcost) {
cost[foot][x][y] = newcost;
q.push(node(foot, x, y, newcost));
}
}
}
}
int best = MAX;
REP(i, 2) REP(j, goal.size()) {
best = min(best, cost[i][goal[j].first][goal[j].second]);
}
if (best == MAX) {
best = -1;
}
cout << best << endl;
}
}
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
{-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int dy[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};
typedef vector<vvi> vvvi;
struct node {
int foot, x, y, c;
node() {}
node(int foot, int x, int y, int c) : foot(foot), x(x), y(y), c(c) {}
};
const int MAX = INT_MAX / 10;
int main() {
int w, h;
while (cin >> w >> h, w && h) {
vvi s(w, vi(h));
vector<pii> start, goal;
REP(j, h) REP(i, w) {
char c;
cin >> c;
switch (c) {
case 'S':
start.push_back(make_pair(i, j));
s[i][j] = 0;
break;
case 'T':
goal.push_back(make_pair(i, j));
s[i][j] = 0;
break;
case 'X':
s[i][j] = -1;
break;
default:
s[i][j] = c - '0';
break;
}
}
vvvi cost(2, vvi(w, vi(h, MAX)));
queue<node> q;
REP(i, 2) REP(j, start.size()) {
q.push(node(i, start[j].first, start[j].second, 0));
cost[i][start[j].first][start[j].second] = 0;
}
while (!q.empty()) {
node n = q.front();
q.pop();
if (cost[n.foot][n.x][n.y] < n.c)
continue;
REP(i, 9) {
int foot = (n.foot + 1) % 2;
int x = n.x + dx[foot][i], y = n.y + dy[foot][i];
if (x >= 0 && y >= 0 && x < w && y < h && s[x][y] >= 0) {
int newcost = cost[n.foot][n.x][n.y] + s[x][y];
if (cost[foot][x][y] > newcost) {
cost[foot][x][y] = newcost;
q.push(node(foot, x, y, newcost));
}
}
}
}
int best = MAX;
REP(i, 2) REP(j, goal.size()) {
best = min(best, cost[i][goal[j].first][goal[j].second]);
}
if (best == MAX) {
best = -1;
}
cout << best << endl;
}
}
|
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 31, 22], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 17, 98], ["+", 0, 52, 15, 339, 51, 34, 12, 16, 12, 22]]
| 1
| 898
|
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef basic_string<bool> BS;
const BS T = BS(1, true);
const BS F = BS(1, false);
char str[999];
char *p;
struct Cell {
Cell *l, *r;
BS bs;
set<BS> sbs;
int num, den;
Cell() : l(), r(), num(), den(1) {
while (*p == ' ') {
p++;
}
if (*p == 'x') {
p++;
bs = T + F;
} else {
p++;
l = new Cell();
r = new Cell();
if (*r > *l) {
swap(l, r);
}
vector<BS> u, i;
set_union(begin(l->sbs), end(l->sbs), begin(r->sbs), end(r->sbs),
back_inserter(u));
set_intersection(begin(l->sbs), end(l->sbs), begin(r->sbs), end(r->sbs),
back_inserter(i));
num = i.size();
den = u.size();
sbs = set<BS>(begin(u), end(u));
bs = T + l->bs + r->bs + F;
}
p++;
sbs.insert(bs);
}
bool operator>(const Cell &c) const {
if (num * c.den != c.num * den) {
return num * c.den < c.num * den;
} else if (l == nullptr && c.l == nullptr) {
return false;
} else if (*l > *c.l || *c.l > *l) {
return *l > *c.l;
} else {
return *r > *c.r;
}
}
void print(bool left) {
if (l == nullptr) {
cout << 'x';
} else {
cout << '(';
if (!left) {
swap(l, r);
}
l->print(true);
r->print(false);
cout << ')';
}
}
~Cell() {
delete l;
delete r;
}
};
int main() {
while (cin.getline(str, 999), str[0] != '0') {
p = str;
Cell c;
c.print(true);
cout << endl;
}
}
|
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef basic_string<bool> BS;
const BS T = BS(1, true);
const BS F = BS(1, false);
char str[999];
char *p;
struct Cell {
Cell *l, *r;
BS bs;
set<BS> sbs;
int num, den;
Cell() : l(), r(), num(), den(1) {
while (*p == ' ') {
p++;
}
if (*p == 'x') {
p++;
bs = T + F;
} else {
p++;
l = new Cell();
r = new Cell();
if (*r > *l) {
swap(l, r);
}
vector<BS> u, i;
set_union(begin(l->sbs), end(l->sbs), begin(r->sbs), end(r->sbs),
back_inserter(u));
set_intersection(begin(l->sbs), end(l->sbs), begin(r->sbs), end(r->sbs),
back_inserter(i));
num = i.size();
den = u.size();
sbs = set<BS>(begin(u), end(u));
bs = T + l->bs + r->bs + F;
}
p++;
sbs.insert(bs);
}
bool operator>(const Cell &c) const {
if (num * c.den != c.num * den) {
return num * c.den < c.num * den;
} else if (l == nullptr && c.l == nullptr) {
return false;
} else if (*l > *c.l || *c.l > *l) {
return *l > *c.l;
} else {
return *r > *c.r;
}
}
void print(bool left) {
if (l == nullptr) {
cout << 'x';
} else {
cout << '(';
if (!left) {
swap(l, r);
}
l->print(true);
cout << ' ';
r->print(false);
cout << ')';
}
}
~Cell() {
delete l;
delete r;
}
};
int main() {
while (cin.getline(str, 999), str[0] != '0') {
p = str;
Cell c;
c.print(true);
cout << endl;
}
}
|
[["+", 75, 76, 0, 9, 0, 1, 0, 16, 31, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 9, 0, 1, 0, 16, 12, 103, 0, 104], ["+", 0, 9, 0, 1, 0, 16, 12, 103, 0, 125], ["+", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
| 1
| 536
|
var n, m;
var a = [], b = [];
function search(x) {
var l = 0, r = m;
while (r - l > 1) {
var c = (l + r) >> 1;
if (b[c] <= x) {
l = c;
} else {
r = c;
}
}
return b[l] === x;
}
function main() {
while (n = scan()) {
m = scan();
rep(n, function(i) { a[i] = scan(); });
rep(m, function(i) { b[i] = scan(); });
a.sort(function(u, v) { return u - v; });
b.sort(function(u, v) { return u - v; });
var s = 0;
rep(n, function(i) { s += a[i]; });
var t = 0;
rep(m, function(i) { t += b[i]; });
var d = Math.floor((t - s) / 2);
if (t - s !== 2 * d) {
print(-1);
continue;
} else {
var ans = false;
rep(n, function(i) {
if (search(a[i] + d)) {
print(a[i] + ' ' + (a[i] + d));
ans = true;
return false;
}
});
if (!ans) {
print(-1);
}
}
a = [];
b = [];
}
}
function rep(a, b, c) {
if (c === undefined) {
c = b;
b = a;
a = 0;
}
for (var i = a; i < b; ++i) {
if (c(i) === false) {
break;
}
}
}
var input = '';
function scan() { return +input.pop(); }
function scan_string() { return input.pop(); }
function print(val) { console.log(val); }
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) { input += chunk; });
process.stdin.on('end', function() {
input = input.trim().split(/\s+/).reverse();
main();
});
|
var n, m;
var a = [], b = [];
function search(x) {
var l = 0, r = m;
while (r - l > 1) {
var c = (l + r) >> 1;
if (b[c] <= x) {
l = c;
} else {
r = c;
}
}
return b[l] === x;
}
function main() {
while (n = scan()) {
m = scan();
rep(n, function(i) { a[i] = scan(); });
rep(m, function(i) { b[i] = scan(); });
a.sort(function(u, v) { return u - v; });
b.sort(function(u, v) { return u - v; });
var s = 0;
rep(n, function(i) { s += a[i]; });
var t = 0;
rep(m, function(i) { t += b[i]; });
var d = Math.floor((t - s) / 2);
if (t - s !== 2 * d) {
print(-1);
} else {
var ans = false;
rep(n, function(i) {
if (search(a[i] + d)) {
print(a[i] + ' ' + (a[i] + d));
ans = true;
return false;
}
});
if (!ans) {
print(-1);
}
}
a = [];
b = [];
}
}
function rep(a, b, c) {
if (c === undefined) {
c = b;
b = a;
a = 0;
}
for (var i = a; i < b; ++i) {
if (c(i) === false) {
break;
}
}
}
var input = '';
function scan() { return +input.pop(); }
function scan_string() { return input.pop(); }
function print(val) { console.log(val); }
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) { input += chunk; });
process.stdin.on('end', function() {
input = input.trim().split(/\s+/).reverse();
main();
});
|
[["-", 8, 556, 0, 57, 64, 556, 0, 116, 0, 117], ["-", 8, 556, 0, 57, 64, 556, 0, 116, 0, 35]]
| 2
| 542
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt();
int m = scanner.nextInt();
if ((n + m) == 0)
break;
int[] t = new int[1000];
int[] h = new int[1000];
int sum_t = 0;
int sum_h = 0;
for (int i = 0; i < n; i++) {
t[i] = scanner.nextInt();
sum_t += t[i];
}
for (int j = 0; j < m; j++) {
h[j] = scanner.nextInt();
sum_h += h[j];
}
int ct = 10000000;
int ch = 10000000;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (sum_t - t[i] + h[j] == sum_h - h[j] + t[i]) {
if (t[i] + h[j] < ct + ch) {
ct = t[i];
ch = h[i];
}
}
}
}
if (ct == 10000000 && ch == 10000000) {
System.out.println("-1");
} else {
System.out.println(ct + " " + ch);
}
}
}
}
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt();
int m = scanner.nextInt();
if ((n + m) == 0)
break;
int[] t = new int[1000];
int[] h = new int[1000];
int sum_t = 0;
int sum_h = 0;
for (int i = 0; i < n; i++) {
t[i] = scanner.nextInt();
sum_t += t[i];
}
for (int j = 0; j < m; j++) {
h[j] = scanner.nextInt();
sum_h += h[j];
}
int ct = 10000000;
int ch = 10000000;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (sum_t - t[i] + h[j] == sum_h - h[j] + t[i]) {
if (t[i] + h[j] < ct + ch) {
ct = t[i];
ch = h[j];
}
}
}
}
if (ct == 10000000 && ch == 10000000) {
System.out.println("-1");
} else {
System.out.println(ct + " " + ch);
}
}
}
}
|
[["-", 64, 196, 0, 1, 0, 11, 12, 504, 71, 22], ["+", 64, 196, 0, 1, 0, 11, 12, 504, 71, 22]]
| 3
| 314
|
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
typedef pair<int, pair<pair<int, int>, int>> P;
// 0 ??????1?????????2?????????3??????
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
int W, H;
while (cin >> W >> H, W || H) {
vector<vector<int>> data;
vector<vector<vector<int>>> M;
data.resize(H);
M.resize(H);
for (size_t i = 0; i < H; i++) {
M[i].resize(W);
data[i].resize(W);
for (size_t j = 0; j < W; j++) {
cin >> data[i][j];
M[i][j].resize(4);
for (size_t k = 0; k < 4; k++) {
M[i][j][k] = INT_MAX;
}
}
}
int C[4] = {};
for (size_t i = 0; i < 4; i++) {
cin >> C[i];
}
priority_queue<P, vector<P>, greater<P>> que;
long long int ans = -1;
que.push(P{0, pair<pair<int, int>, int>{pair<int, int>{0, 0}, 0}});
M[0][0][0] = 0;
while (!que.empty()) {
P now = que.top();
que.pop();
int nowcost = now.first;
int nowx = now.second.first.second;
int nowy = now.second.first.first;
int nowd = now.second.second;
// cout << nowx << " " << nowy << " " << nowcost << endl;
/*if( nowy == H - 1 && nowx == W - 1 )
{
ans = nowcost;
break;
}*/
// if( data[nowy][nowx] == 4 )continue;
for (size_t i = 0; i < 4; i++) {
int nextd = (i + nowd) % 4;
int nextx = nowx + dx[nextd];
int nexty = nowy + dy[nextd];
if (nextx < 0 || nextx >= W || nexty < 0 || nexty >= H)
continue;
if (i == data[nowy][nowx]) {
if (M[nexty][nextx][i] > nowcost) {
M[nexty][nextx][i] = nowcost;
que.push(P{nowcost, pair<pair<int, int>, int>{
pair<int, int>{nexty, nextx}, nextd}});
}
} else {
if (M[nexty][nextx][i] > nowcost + C[i]) {
M[nexty][nextx][i] = nowcost + C[i];
que.push(P{nowcost + C[i],
pair<pair<int, int>, int>{pair<int, int>{nexty, nextx},
nextd}});
}
}
}
}
// cout << ans << endl;
int ans2 = INT_MAX;
for (size_t i = 0; i < 4; i++) {
ans2 = min(ans2, M[H - 1][W - 1][i]);
}
cout << ans2 << endl;
}
}
|
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
typedef pair<int, pair<pair<int, int>, int>> P;
// 0 ??????1?????????2?????????3??????
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
int W, H;
while (cin >> W >> H, W || H) {
vector<vector<int>> data;
vector<vector<vector<int>>> M;
data.resize(H);
M.resize(H);
for (size_t i = 0; i < H; i++) {
M[i].resize(W);
data[i].resize(W);
for (size_t j = 0; j < W; j++) {
cin >> data[i][j];
M[i][j].resize(4);
for (size_t k = 0; k < 4; k++) {
M[i][j][k] = INT_MAX;
}
}
}
int C[4] = {};
for (size_t i = 0; i < 4; i++) {
cin >> C[i];
}
priority_queue<P, vector<P>, greater<P>> que;
long long int ans = -1;
que.push(P{0, pair<pair<int, int>, int>{pair<int, int>{0, 0}, 0}});
M[0][0][0] = 0;
while (!que.empty()) {
P now = que.top();
que.pop();
int nowcost = now.first;
int nowx = now.second.first.second;
int nowy = now.second.first.first;
int nowd = now.second.second;
// cout << nowx << " " << nowy << " " << nowcost << endl;
/*if( nowy == H - 1 && nowx == W - 1 )
{
ans = nowcost;
break;
}*/
// if( data[nowy][nowx] == 4 )continue;
for (size_t i = 0; i < 4; i++) {
int nextd = (i + nowd) % 4;
int nextx = nowx + dx[nextd];
int nexty = nowy + dy[nextd];
if (nextx < 0 || nextx >= W || nexty < 0 || nexty >= H)
continue;
if (i == data[nowy][nowx]) {
if (M[nexty][nextx][nextd] > nowcost) {
M[nexty][nextx][nextd] = nowcost;
que.push(P{nowcost, pair<pair<int, int>, int>{
pair<int, int>{nexty, nextx}, nextd}});
}
} else {
if (M[nexty][nextx][nextd] > nowcost + C[i]) {
M[nexty][nextx][nextd] = nowcost + C[i];
que.push(P{nowcost + C[i],
pair<pair<int, int>, int>{pair<int, int>{nexty, nextx},
nextd}});
}
}
}
}
// cout << ans << endl;
int ans2 = INT_MAX;
for (size_t i = 0; i < 4; i++) {
ans2 = min(ans2, M[H - 1][W - 1][i]);
}
cout << ans2 << endl;
}
}
|
[["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
| 1
| 830
|
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, pair<int, int>> pip;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int area[31][31];
int min_cost[31][31][4];
int c[4];
void bfs() {
queue<pip> qp;
qp.push(make_pair(0, make_pair(1, 1)));
while (!qp.empty()) {
pip p = qp.front();
qp.pop();
int y = p.second.first;
int x = p.second.second;
int d = p.first;
for (int i = 0; i < 4; i++) {
int y2 = y + dy[(d + i) % 4];
int x2 = x + dx[(d + i) % 4];
if (y2 < 1 || y2 > h || x2 < 1 || x2 > w)
continue;
int cost = 0;
int d2 = (d + i) % 4;
if (area[y][x] != i)
cost = c[i];
if (min_cost[y2][x2][d2] < 0 ||
min_cost[y][x][d] + cost < min_cost[y2][x2][d2]) {
min_cost[y2][x2][d2] = min_cost[y][x][d] + cost;
qp.push(make_pair(d2, make_pair(y2, x2)));
}
}
}
}
int main() {
while (cin >> w >> h && (w || h)) {
memset(min_cost, -1, sizeof(min_cost));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> area[i][j];
}
}
for (int i = 0; i < 4; i++) {
cin >> c[i];
}
min_cost[1][1][0] = 0;
bfs();
int cost = 1 << 15;
for (int i = 0; i < 4; i++) {
if (min_cost[h][w][i] > 0)
cost = min(cost, min_cost[h][w][i]);
}
if (cost == 1 << 15)
cout << -1 << endl;
else
cout << cost << endl;
}
}
|
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, pair<int, int>> pip;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int area[31][31];
int min_cost[31][31][4];
int c[4];
void bfs() {
queue<pip> qp;
qp.push(make_pair(0, make_pair(1, 1)));
while (!qp.empty()) {
pip p = qp.front();
qp.pop();
int y = p.second.first;
int x = p.second.second;
int d = p.first;
for (int i = 0; i < 4; i++) {
int y2 = y + dy[(d + i) % 4];
int x2 = x + dx[(d + i) % 4];
if (y2 < 1 || y2 > h || x2 < 1 || x2 > w)
continue;
int cost = 0;
int d2 = (d + i) % 4;
if (area[y][x] != i)
cost = c[i];
if (min_cost[y2][x2][d2] < 0 ||
min_cost[y][x][d] + cost < min_cost[y2][x2][d2]) {
min_cost[y2][x2][d2] = min_cost[y][x][d] + cost;
qp.push(make_pair(d2, make_pair(y2, x2)));
}
}
}
}
int main() {
while (cin >> w >> h && (w || h)) {
memset(min_cost, -1, sizeof(min_cost));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> area[i][j];
}
}
for (int i = 0; i < 4; i++) {
cin >> c[i];
}
min_cost[1][1][0] = 0;
bfs();
int cost = 1 << 15;
for (int i = 0; i < 4; i++) {
if (min_cost[h][w][i] >= 0)
cost = min(cost, min_cost[h][w][i]);
}
if (cost == 1 << 15)
cout << -1 << endl;
else
cout << cost << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
| 1
| 558
|
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int c[4];
struct State {
int x, y, d, cost;
};
bool operator<(const State a, State b) { return a.cost > b.cost; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void calc(int w, int h) {
int direction[30][30];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> direction[j][i];
for (int i = 0; i < 4; i++)
cin >> c[i];
priority_queue<State> Q;
bool costmap[30][30][4];
for (int i = 0; i < 30; i++)
for (int j = 0; j < 30; j++)
for (int z = 0; z < 4; z++)
costmap[i][j][z] = false;
costmap[0][0][0] = true;
State first;
first.x = first.y = first.cost = first.d = 0;
Q.push(first);
State k;
while (!Q.empty()) {
k = Q.top();
Q.pop();
costmap[k.x][k.y][k.d] = true;
if (k.x == w - 1 && k.y == h - 1)
break;
for (int i = 0; i < 4; i++) {
int tx, ty, td, tcost;
State temp;
td = (k.d + i) % 4;
tx = k.x + dx[td];
ty = k.y + dy[td];
if (tx < 0 || ty < 0 || tx > w || ty > h)
continue;
tcost = k.cost;
if (direction[k.x][k.y] != i)
tcost += c[i];
temp.x = tx;
temp.y = ty;
temp.d = td;
temp.cost = tcost;
if (!costmap[temp.x][temp.y][temp.d])
Q.push(temp);
}
}
cout << k.cost << "\n";
}
int main() {
int w, h;
cin >> w >> h;
while (w != 0) {
calc(w, h);
cin >> w >> h;
}
return 0;
}
|
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int c[4];
struct State {
int x, y, d, cost;
};
bool operator<(const State a, State b) { return a.cost > b.cost; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void calc(int w, int h) {
int direction[30][30];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> direction[j][i];
for (int i = 0; i < 4; i++)
cin >> c[i];
priority_queue<State> Q;
bool costmap[30][30][4];
for (int i = 0; i < 30; i++)
for (int j = 0; j < 30; j++)
for (int z = 0; z < 4; z++)
costmap[i][j][z] = false;
costmap[0][0][0] = true;
State first;
first.x = first.y = first.cost = first.d = 0;
Q.push(first);
State k;
while (!Q.empty()) {
k = Q.top();
Q.pop();
costmap[k.x][k.y][k.d] = true;
if (k.x == w - 1 && k.y == h - 1)
break;
for (int i = 0; i < 4; i++) {
int tx, ty, td, tcost;
State temp;
td = (k.d + i) % 4;
tx = k.x + dx[td];
ty = k.y + dy[td];
if (tx < 0 || ty < 0 || tx >= w || ty >= h)
continue;
tcost = k.cost;
if (direction[k.x][k.y] != i)
tcost += c[i];
temp.x = tx;
temp.y = ty;
temp.d = td;
temp.cost = tcost;
if (!costmap[temp.x][temp.y][temp.d])
Q.push(temp);
}
}
cout << k.cost << "\n";
}
int main() {
int w, h;
cin >> w >> h;
while (w != 0) {
calc(w, h);
cin >> w >> h;
}
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 47], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 20], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20]]
| 1
| 557
|
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
const double EPS = 1e-9;
// 0: "Straight"
// 1: "Right"
// 2: "Back"
// 3: "Left"
// 4: "Halt"
const int dx[][4] = {
// → ↓ ← ↑
{1, 0, -1, 0},
// ↑ → ↓ ←
{0, 1, 0, -1},
// ← ↑ → ↓
{-1, 0, 1, 0},
// ↓ ← ↑ →
{0, -1, 0, 1}};
const int dy[][4] = {
// → ↓ ← ↑
{0, 1, 0, -1},
// ↑ → ↓ ←
{-1, 0, 1, 0},
// ← ↑ → ↓
{0, -1, 0, 1},
// ↓ ← ↑ →
{1, 0, -1, 0}};
// → ↑ ← ↓
const int dd[][4] = {
// → ↓ ← ↑
{0, 3, 2, 1},
// ↑ → ↓ ←
{1, 0, 1, 3},
// ← ↑ → ↓
{2, 1, 0, 3},
// ↓ ← ↑ →
{3, 2, 1, 0}};
struct Robot {
int y, x, dir, step;
Robot(int y, int x, int dir, int step) : y(y), x(x), dir(dir), step(step){};
};
bool operator<(const Robot &lhs, const Robot &rhs) {
return lhs.step < rhs.step;
}
bool operator>(const Robot &lhs, const Robot &rhs) {
return lhs.step > rhs.step;
}
int main() {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 and h == 0)
break;
int gy = h - 1, gx = w - 1;
vector<vector<int>> field(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> field[i][j];
}
}
vector<int> costs(4);
for (int i = 0; i < 4; i++)
cin >> costs[i];
vector<vector<vector<char>>> used(
h, vector<vector<char>>(w, vector<char>(4, false)));
priority_queue<Robot, vector<Robot>, greater<Robot>> que;
que.push(Robot(0, 0, 0, 0));
int ret = 0;
while (not que.empty()) {
Robot r = que.top();
que.pop();
if (used[r.y][r.x][r.dir])
continue;
used[r.y][r.x][r.dir] = true;
if (r.y == gy and r.x == gx) {
ret = r.step;
break;
}
for (int i = 0; i < 4; i++) {
int ny = r.y + dy[r.dir][i];
int nx = r.x + dx[r.dir][i];
if (ny < 0 or ny >= h or nx < 0 or nx >= w)
continue;
if (i == field[r.y][r.x]) {
que.push(Robot(ny, nx, dd[r.dir][i], r.step));
} else {
que.push(Robot(ny, nx, dd[r.dir][i], r.step + costs[i]));
}
}
}
cout << ret << endl;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;
const double EPS = 1e-9;
// 0: "Straight"
// 1: "Right"
// 2: "Back"
// 3: "Left"
// 4: "Halt"
const int dx[][4] = {
// → ↓ ← ↑
{1, 0, -1, 0},
// ↑ → ↓ ←
{0, 1, 0, -1},
// ← ↑ → ↓
{-1, 0, 1, 0},
// ↓ ← ↑ →
{0, -1, 0, 1}};
const int dy[][4] = {
// → ↓ ← ↑
{0, 1, 0, -1},
// ↑ → ↓ ←
{-1, 0, 1, 0},
// ← ↑ → ↓
{0, -1, 0, 1},
// ↓ ← ↑ →
{1, 0, -1, 0}};
// → ↑ ← ↓
const int dd[][4] = {
// → ↓ ← ↑
{0, 3, 2, 1},
// ↑ → ↓ ←
{1, 0, 3, 2},
// ← ↑ → ↓
{2, 1, 0, 3},
// ↓ ← ↑ →
{3, 2, 1, 0}};
struct Robot {
int y, x, dir, step;
Robot(int y, int x, int dir, int step) : y(y), x(x), dir(dir), step(step){};
};
bool operator<(const Robot &lhs, const Robot &rhs) {
return lhs.step < rhs.step;
}
bool operator>(const Robot &lhs, const Robot &rhs) {
return lhs.step > rhs.step;
}
int main() {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 and h == 0)
break;
int gy = h - 1, gx = w - 1;
vector<vector<int>> field(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> field[i][j];
}
}
vector<int> costs(4);
for (int i = 0; i < 4; i++)
cin >> costs[i];
vector<vector<vector<char>>> used(
h, vector<vector<char>>(w, vector<char>(4, false)));
priority_queue<Robot, vector<Robot>, greater<Robot>> que;
que.push(Robot(0, 0, 0, 0));
int ret = 0;
while (not que.empty()) {
Robot r = que.top();
que.pop();
if (used[r.y][r.x][r.dir])
continue;
used[r.y][r.x][r.dir] = true;
if (r.y == gy and r.x == gx) {
ret = r.step;
break;
}
for (int i = 0; i < 4; i++) {
int ny = r.y + dy[r.dir][i];
int nx = r.x + dx[r.dir][i];
if (ny < 0 or ny >= h or nx < 0 or nx >= w)
continue;
if (i == field[r.y][r.x]) {
que.push(Robot(ny, nx, dd[r.dir][i], r.step));
} else {
que.push(Robot(ny, nx, dd[r.dir][i], r.step + costs[i]));
}
}
}
cout << ret << endl;
}
return 0;
}
|
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 21], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13]]
| 1
| 820
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, b;
int main() {
while (1) {
cin >> n >> b;
if (n == 0)
return 0;
int index = 0;
int s[n];
for (int i = 0; i < n; i++) {
s[n] = 0;
}
int stone = b;
while (1) {
if (stone != 0) {
stone--;
s[index] += 1;
} else if (stone == 0) {
stone += s[index];
s[index] = 0;
}
if (s[index] == b) {
break;
}
if (index == n - 1) {
index = 0;
} else {
index++;
}
}
cout << index << endl;
}
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sort(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define fi first
#define se second
#define inf 999999999
using namespace std;
typedef pair<int, int> p;
typedef pair<ll, ll> lp;
bool is_uruu(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------//
int n, b;
int main() {
while (1) {
cin >> n >> b;
if (n == 0)
return 0;
int index = 0;
int s[n];
for (int i = 0; i < n; i++) {
s[i] = 0;
}
int stone = b;
while (1) {
if (stone != 0) {
stone--;
s[index] += 1;
} else if (stone == 0) {
stone += s[index];
s[index] = 0;
}
if (s[index] == b) {
break;
}
if (index == n - 1) {
index = 0;
} else {
index++;
}
}
cout << index << endl;
}
}
|
[["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
| 1
| 282
|
#include <iostream>
using namespace std;
int main() {
int P, N, ndata[55], wan, lop;
while (1) {
cin >> N;
cin >> P;
if (N == 0 && P == 0) {
break;
}
for (lop = 0; lop < N; lop++) {
ndata[lop] = 0;
}
lop = 0;
wan = P;
while (1) {
if (N == 3 && P == 3) {
cout << "3" << endl;
break;
}
for (lop = 0; lop < N; lop++) {
if (wan != 0) {
wan--;
ndata[lop]++;
} else {
wan = wan + ndata[lop];
ndata[lop] = 0;
}
/*cout<<"wan="<<wan<<endl;
for(lop=0;lop<N;lop++){
cout<<lop<<" "<<ndata[lop]<<endl;
}
cout<<endl;*/
if (ndata[lop] == P && wan == 0) {
break;
}
}
if (ndata[lop] == P && wan == 0) {
cout << lop << endl;
break;
}
}
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int P, N, ndata[55], wan, lop;
while (1) {
cin >> N;
cin >> P;
if (N == 0 && P == 0) {
break;
}
for (lop = 0; lop < N; lop++) {
ndata[lop] = 0;
}
lop = 0;
wan = P;
while (1) {
if (N == 3 && P == 3) {
lop = 0;
cout << lop << endl;
break;
}
for (lop = 0; lop < N; lop++) {
if (wan != 0) {
wan--;
ndata[lop]++;
} else {
wan = wan + ndata[lop];
ndata[lop] = 0;
}
/*cout<<"wan="<<wan<<endl;
for(lop=0;lop<N;lop++){
cout<<lop<<" "<<ndata[lop]<<endl;
}
cout<<endl;*/
if (ndata[lop] == P && wan == 0) {
break;
}
}
if (ndata[lop] == P && wan == 0) {
cout << lop << endl;
break;
}
}
}
return 0;
}
|
[["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 9, 0, 1, 0, 16, 31, 16, 12, 22]]
| 1
| 207
|
#include <iostream>
using namespace std;
int myMap[50][50];
int w, h;
int ans;
bool safe(int x, int y) { return (0 <= x && 0 <= y) && (x < w && y < h); }
void del(int x, int y) {
myMap[x][y] = 0;
if (safe(x + 1, y) && myMap[x + 1][y])
del(x + 1, y);
if (safe(x + 1, y + 1) && myMap[x + 1][y + 1])
del(x + 1, y + 1);
if (safe(x + 1, y - 1) && myMap[x + 1][y - 1])
del(x + 1, y - 1);
if (safe(x, y + 1) && myMap[x][y + 1])
del(x, y + 1);
if (safe(x, y - 1) && myMap[x][y - 1])
del(x, y - 1);
if (safe(x - 1, y - 1) && myMap[x - 1][y - 1])
del(x - 1, y - 1);
if (safe(x - 1, y + 1) && myMap[x - 1][y + 1])
del(x - 1, y + 1);
if (safe(x - 1, y) && myMap[x - 1][y])
del(x - 1, y);
return;
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> myMap[i][j];
}
}
ans = 0;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if (myMap[i][j] == 1) {
ans++;
del(i, j);
}
}
}
cout << ans << endl;
}
}
|
#include <iostream>
using namespace std;
int myMap[51][51];
int w, h;
int ans;
bool safe(int x, int y) { return (0 <= x && 0 <= y) && (x < h && y < w); }
void del(int x, int y) {
myMap[x][y] = 0;
if (safe(x + 1, y) && myMap[x + 1][y])
del(x + 1, y);
if (safe(x + 1, y + 1) && myMap[x + 1][y + 1])
del(x + 1, y + 1);
if (safe(x + 1, y - 1) && myMap[x + 1][y - 1])
del(x + 1, y - 1);
if (safe(x, y + 1) && myMap[x][y + 1])
del(x, y + 1);
if (safe(x, y - 1) && myMap[x][y - 1])
del(x, y - 1);
if (safe(x - 1, y - 1) && myMap[x - 1][y - 1])
del(x - 1, y - 1);
if (safe(x - 1, y + 1) && myMap[x - 1][y + 1])
del(x - 1, y + 1);
if (safe(x - 1, y) && myMap[x - 1][y])
del(x - 1, y);
return;
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> myMap[i][j];
}
}
ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (myMap[i][j] == 1) {
ans++;
del(i, j);
}
}
}
cout << ans << endl;
}
}
|
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 16, 12, 23, 0, 16, 31, 16, 12, 22], ["+", 0, 16, 12, 23, 0, 16, 31, 16, 12, 22], ["-", 0, 16, 12, 23, 0, 16, 12, 16, 12, 22], ["+", 0, 16, 12, 23, 0, 16, 12, 16, 12, 22], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
| 1
| 480
|
#include <iostream>
using namespace std;
int w, h;
int m[50][50];
void solve(int x, int y) {
if (m[y][x] == 1) {
m[y][x] = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) {
continue;
}
if (y + i >= 0 && y + i < h && x + i >= 0 && x + i < w) {
solve(x + j, y + i);
}
}
}
}
}
int main() {
while (true) {
int count = 0;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> m[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (m[i][j] == 1) {
solve(j, i);
count++;
}
}
}
cout << count << endl;
}
}
|
#include <iostream>
using namespace std;
int w, h;
int m[50][50];
void solve(int x, int y) {
if (m[y][x] == 1) {
m[y][x] = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) {
continue;
}
if (y + i >= 0 && y + i < h && x + j >= 0 && x + j < w) {
solve(x + j, y + i);
}
}
}
}
}
int main() {
while (true) {
int count = 0;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> m[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (m[i][j] == 1) {
solve(j, i);
count++;
}
}
}
cout << count << endl;
}
}
|
[["-", 51, 16, 31, 16, 12, 16, 31, 16, 12, 22], ["+", 51, 16, 31, 16, 12, 16, 31, 16, 12, 22], ["-", 15, 339, 51, 16, 12, 16, 31, 16, 12, 22], ["+", 15, 339, 51, 16, 12, 16, 31, 16, 12, 22]]
| 1
| 281
|
#include <cstdio>
using namespace std;
int w, h;
int field[50][51];
void dfs(int x, int y) {
field[x][y] = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx;
int ny = y + dy;
if (0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] == 1)
dfs(nx, ny);
}
}
return;
}
void solve(void) {
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (field[i][j] == 1) {
dfs(i, j);
res++;
}
}
}
printf("%d\n", res);
}
int main(void) {
while (scanf("%d %d", &w, &h) != EOF) {
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &field[i][j]);
}
}
solve();
}
return 0;
}
|
#include <cstdio>
using namespace std;
int w, h;
int field[50][51];
void dfs(int x, int y) {
field[x][y] = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx;
int ny = y + dy;
if (0 <= nx && nx < h && 0 <= ny && ny < w && field[nx][ny] == 1)
dfs(nx, ny);
}
}
return;
}
void solve(void) {
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (field[i][j] == 1) {
dfs(i, j);
res++;
}
}
}
printf("%d\n", res);
}
int main(void) {
while (scanf("%d %d", &w, &h) != EOF) {
if (w == 0 && h == 0) {
break;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d", &field[i][j]);
}
}
solve();
}
return 0;
}
|
[["-", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["+", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["-", 15, 339, 51, 16, 31, 16, 12, 16, 12, 22], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 12, 22]]
| 1
| 297
|
#include <iostream>
using namespace std;
int ans;
int map[100][100];
void dfs(int x, int y, int w, int h);
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
int w, h;
while (cin >> w >> h) {
ans = 0;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> map[i][j];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
ans++;
dfs(i, j, w, h);
}
}
// cout<<"map"<<endl;
// for(int a=0;a<h;a++){
// for(int b=0;b<w;b++) cout<<map[a][b];
// cout<<endl;
// }
// cout<<endl;
cout << ans << endl;
}
return 0;
}
void dfs(int x, int y, int w, int h) {
// cout<<"LK+LKJ"<<endl;
int nx, ny;
for (int i = 0; i < 8; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] == 1) {
map[ny][nx] = 0;
dfs(nx, ny, w, h);
}
}
}
|
#include <iostream>
using namespace std;
int ans;
int map[100][100];
void dfs(int x, int y, int w, int h);
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
int w, h;
while (cin >> w >> h) {
ans = 0;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> map[i][j];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
ans++;
dfs(j, i, w, h);
}
}
// cout<<"map"<<endl;
// for(int a=0;a<h;a++){
// for(int b=0;b<w;b++) cout<<map[a][b];
// cout<<endl;
// }
// cout<<endl;
cout << ans << endl;
}
return 0;
}
void dfs(int x, int y, int w, int h) {
// cout<<"LK+LKJ"<<endl;
int nx, ny;
for (int i = 0; i < 8; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h && map[ny][nx] == 1) {
map[ny][nx] = 0;
dfs(nx, ny, w, h);
}
}
}
|
[["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 347
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
class Prime():
def __init__(self, n):
self.M = m = int(math.sqrt(n)) + 10
self.A = a = [True] * m
a[0] = a[1] = False
self.T = t = []
for i in range(2, m):
if not a[i]:
continue
if not (i % 7 == 1 or i % 7 == 6):
continue
t.append(i)
for j in range(i*i,m,i):
a[j] = False
def is_prime(self, n):
return self.A[n]
def division(self, n):
d = []
for c in self.T:
if n < c:
break
if n % c == 0:
d.append(c)
return d
def main():
rr = []
pr = Prime(300000**2)
while True:
n = I()
if n == 1:
break
d = pr.division(n)
rr.append('{}:{}'.format(n,' '.join(map(str,d))))
return '\n'.join(map(str, rr))
print(main())
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
class Prime():
def __init__(self, n):
self.M = m = int(math.sqrt(n)) + 10
self.A = a = [True] * m
a[0] = a[1] = False
self.T = t = []
for i in range(2, m):
if not a[i]:
continue
if not (i % 7 == 1 or i % 7 == 6):
continue
t.append(i)
for j in range(i*i,m,i):
a[j] = False
def is_prime(self, n):
return self.A[n]
def division(self, n):
d = []
for c in self.T:
if n < c:
break
if n % c == 0:
d.append(c)
return d
def main():
rr = []
pr = Prime(300000**2)
while True:
n = I()
if n == 1:
break
d = pr.division(n)
rr.append('{}: {}'.format(n,' '.join(map(str,d))))
return '\n'.join(map(str, rr))
print(main())
|
[["-", 3, 4, 0, 652, 63, 319, 500, 557, 0, 6], ["+", 3, 4, 0, 652, 63, 319, 500, 557, 0, 6]]
| 5
| 466
|
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static interface Fomula { int eval(int[] input); }
public static class Minus implements Fomula {
Fomula inner;
public Minus(Fomula in) { inner = in; }
@Override
public int eval(int[] input) {
return 2 - inner.eval(input);
}
public String toString() { return "-" + inner; }
}
public static class Add implements Fomula {
Fomula left, right;
public Add(Fomula left, Fomula right) {
this.left = left;
this.right = right;
}
@Override
public int eval(int[] input) {
return Math.max(left.eval(input), right.eval(input));
}
public String toString() { return "(" + left + "+" + right + ")"; }
}
public static class Mul implements Fomula {
Fomula left, right;
public Mul(Fomula left, Fomula right) {
this.left = left;
this.right = right;
}
@Override
public int eval(int[] input) {
return Math.min(left.eval(input), right.eval(input));
}
public String toString() { return "(" + left + "*" + right + ")"; }
}
public static class Const implements Fomula {
int con;
public Const(int con) { this.con = con; }
@Override
public int eval(int[] input) {
return con;
}
public String toString() { return "" + con; }
}
public static class Var implements Fomula {
int var;
public Var(int v) { var = v; }
@Override
public int eval(int[] input) {
return input[var];
}
public String toString() { return (char)(var + 'P') + ""; }
}
public static Fomula parse(char[] input, int start, int end) {
int k_lv = 0;
int k_s = -1, k_e = -1;
int m_s = -1;
int a_p = -1, m_p = -1;
for (int pos = start; pos <= end; pos++) {
if (input[pos] == '(') {
if (k_lv == 0) {
k_s = pos;
}
k_lv++;
} else if (input[pos] == ')') {
k_lv--;
if (k_lv == 0) {
k_e = pos;
break;
}
} else if (input[pos] == '-' && k_lv == 0 && m_s == -1) {
m_s = pos;
} else if (input[pos] == '*' && k_lv == 1) {
m_p = pos;
} else if (input[pos] == '+' && k_lv == 1) {
a_p = pos;
}
}
if (k_s != -1 && k_e != -1) {
if (m_p != -1) {
return new Mul(parse(input, k_s + 1, m_p - 1),
parse(input, m_p + 1, k_e - 1));
} else {
return new Add(parse(input, k_s + 1, a_p - 1),
parse(input, a_p + 1, k_e - 1));
}
} else if (m_s != -1) {
return new Minus(parse(input, m_s + 1, end));
} else if ('0' <= input[start] && input[start] <= '2') {
return new Const(input[start] - '0');
} else {
return new Var(input[start] - 'P');
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.next();
if (".".equals(str)) {
break;
}
char[] input = str.toCharArray();
Fomula fomula = parse(input, 0, input.length - 1);
// System.out.println(fomula);
int count = 0;
int[] array = new int[3];
for (int p = 0; p <= 2; p++) {
array[0] = p;
for (int q = 0; q <= 2; q++) {
array[1] = q;
for (int r = 0; r <= 2; r++) {
array[2] = r;
if (fomula.eval(array) == 2) {
count++;
}
}
}
}
System.out.println(count);
}
}
}
|
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static interface Fomula { int eval(int[] input); }
public static class Minus implements Fomula {
Fomula inner;
public Minus(Fomula in) { inner = in; }
@Override
public int eval(int[] input) {
return 2 - inner.eval(input);
}
public String toString() { return "-" + inner; }
}
public static class Add implements Fomula {
Fomula left, right;
public Add(Fomula left, Fomula right) {
this.left = left;
this.right = right;
}
@Override
public int eval(int[] input) {
return Math.max(left.eval(input), right.eval(input));
}
public String toString() { return "(" + left + "+" + right + ")"; }
}
public static class Mul implements Fomula {
Fomula left, right;
public Mul(Fomula left, Fomula right) {
this.left = left;
this.right = right;
}
@Override
public int eval(int[] input) {
return Math.min(left.eval(input), right.eval(input));
}
public String toString() { return "(" + left + "*" + right + ")"; }
}
public static class Const implements Fomula {
int con;
public Const(int con) { this.con = con; }
@Override
public int eval(int[] input) {
return con;
}
public String toString() { return "" + con; }
}
public static class Var implements Fomula {
int var;
public Var(int v) { var = v; }
@Override
public int eval(int[] input) {
return input[var];
}
public String toString() { return (char)(var + 'P') + ""; }
}
public static Fomula parse(char[] input, int start, int end) {
int k_lv = 0;
int k_s = -1, k_e = -1;
int m_s = -1;
int a_p = -1, m_p = -1;
for (int pos = start; pos <= end; pos++) {
if (input[pos] == '(') {
if (k_lv == 0) {
k_s = pos;
}
k_lv++;
} else if (input[pos] == ')') {
k_lv--;
if (k_lv == 0) {
k_e = pos;
break;
}
} else if (input[pos] == '-' && k_lv == 0 && m_s == -1) {
m_s = pos;
break;
} else if (input[pos] == '*' && k_lv == 1) {
m_p = pos;
} else if (input[pos] == '+' && k_lv == 1) {
a_p = pos;
}
}
if (k_s != -1 && k_e != -1) {
if (m_p != -1) {
return new Mul(parse(input, k_s + 1, m_p - 1),
parse(input, m_p + 1, k_e - 1));
} else {
return new Add(parse(input, k_s + 1, a_p - 1),
parse(input, a_p + 1, k_e - 1));
}
} else if (m_s != -1) {
return new Minus(parse(input, m_s + 1, end));
} else if ('0' <= input[start] && input[start] <= '2') {
return new Const(input[start] - '0');
} else {
return new Var(input[start] - 'P');
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.next();
if (".".equals(str)) {
break;
}
char[] input = str.toCharArray();
Fomula fomula = parse(input, 0, input.length - 1);
// System.out.println(fomula);
int count = 0;
int[] array = new int[3];
for (int p = 0; p <= 2; p++) {
array[0] = p;
for (int q = 0; q <= 2; q++) {
array[1] = q;
for (int r = 0; r <= 2; r++) {
array[2] = r;
if (fomula.eval(array) == 2) {
count++;
}
}
}
}
System.out.println(count);
}
}
}
|
[["+", 75, 57, 75, 57, 64, 196, 0, 93, 0, 94], ["+", 75, 57, 75, 57, 64, 196, 0, 93, 0, 35]]
| 3
| 948
|
#include <stdio.h>
#include <string.h>
static const char *e;
static int p, q, r;
static int expr(void);
static int term(void) {
int res;
switch (*(e++)) {
case '-':
res = expr();
switch (res) {
case 0:
res = 2;
break;
case 2:
res = 0;
break;
}
break;
case 'P':
res = p;
break;
case 'Q':
res = q;
break;
case 'R':
res = r;
break;
case '0':
res = 0;
break;
case '1':
res = 1;
break;
case '2':
res = 2;
break;
}
return (res);
}
static int nanikaka(void) {
int lt, rt;
char opr;
int res;
lt = expr();
opr = *(e++);
rt = expr();
switch (opr) {
case '*':
res = 1;
if (lt == 0 || rt == 0)
res = 1;
if (lt == 2 && rt == 2)
res = 2;
break;
case '+':
res = 0;
if (lt == 1 || rt == 1)
res = 1;
if (lt == 2 || rt == 2)
res = 2;
break;
}
return (res);
}
static int expr(void) {
int lt, rt, res;
switch (*e) {
case '(':
++e;
res = nanikaka();
++e;
break;
default:
res = term();
break;
}
return (res);
}
/** Application main entry point. */
int main(int argc, char *argv[]) {
char s[128];
for (;;) {
int res = 0;
scanf(" %s", s);
if (!strcmp(s, "."))
break;
for (p = 0; p <= 2; ++p)
for (q = 0; q <= 2; ++q)
for (r = 0; r <= 2; ++r) {
e = s;
res += !!(expr() == 2);
}
printf("%d\n", res);
}
return (0);
}
|
#include <stdio.h>
#include <string.h>
static const char *e;
static int p, q, r;
static int expr(void);
static int term(void) {
int res;
switch (*(e++)) {
case '-':
res = expr();
switch (res) {
case 0:
res = 2;
break;
case 2:
res = 0;
break;
}
break;
case 'P':
res = p;
break;
case 'Q':
res = q;
break;
case 'R':
res = r;
break;
case '0':
res = 0;
break;
case '1':
res = 1;
break;
case '2':
res = 2;
break;
}
return (res);
}
static int nanikaka(void) {
int lt, rt;
char opr;
int res;
lt = expr();
opr = *(e++);
rt = expr();
switch (opr) {
case '*':
res = 1;
if (lt == 0 || rt == 0)
res = 0;
if (lt == 2 && rt == 2)
res = 2;
break;
case '+':
res = 0;
if (lt == 1 || rt == 1)
res = 1;
if (lt == 2 || rt == 2)
res = 2;
break;
}
return (res);
}
static int expr(void) {
int lt, rt, res;
switch (*e) {
case '(':
++e;
res = nanikaka();
++e;
break;
default:
res = term();
break;
}
return (res);
}
/** Application main entry point. */
int main(int argc, char *argv[]) {
char s[128];
for (;;) {
int res = 0;
scanf(" %s", s);
if (!strcmp(s, "."))
break;
for (p = 0; p <= 2; ++p)
for (q = 0; q <= 2; ++q)
for (r = 0; r <= 2; ++r) {
e = s;
res += !!(expr() == 2);
}
printf("%d\n", res);
}
return (0);
}
|
[["-", 0, 100, 0, 57, 64, 1, 0, 11, 12, 13], ["+", 0, 100, 0, 57, 64, 1, 0, 11, 12, 13]]
| 0
| 466
|
#include <bits/stdc++.h>
using namespace std;
string str;
int pos, P, Q, R;
int formula() {
pos = -1;
if (str[pos] == '(') {
int f = formula();
pos++;
if (str[pos] == '*')
f = min(f, formula());
if (str[pos] == '+')
f = max(f, formula());
pos++;
return f;
}
if (str[pos] == '-')
return 2 - formula();
if (str[pos] == 'P')
return P;
if (str[pos] == 'Q')
return Q;
if (str[pos] == 'R')
return R;
if (isdigit(str[pos]))
return str[pos] - '0';
}
int main() {
while (cin >> str) {
if (str == ".")
return 0;
int cnt = 0;
for (P = 0; P <= 2; P++)
for (Q = 0; Q <= 2; Q++)
for (R = 0; R <= 2; R++) {
pos = -1;
cnt += formula() == 2;
}
cout << cnt << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string str;
int pos, P, Q, R;
int formula() {
pos++;
if (str[pos] == '(') {
int f = formula();
pos++;
if (str[pos] == '*')
f = min(f, formula());
if (str[pos] == '+')
f = max(f, formula());
pos++;
return f;
}
if (str[pos] == '-')
return 2 - formula();
if (str[pos] == 'P')
return P;
if (str[pos] == 'Q')
return Q;
if (str[pos] == 'R')
return R;
if (isdigit(str[pos]))
return str[pos] - '0';
}
int main() {
while (cin >> str) {
if (str == ".")
return 0;
int cnt = 0;
for (P = 0; P <= 2; P++)
for (Q = 0; Q <= 2; Q++)
for (R = 0; R <= 2; R++) {
pos = -1;
cnt += formula() == 2;
}
cout << cnt << endl;
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 1, 0, 11, 17, 32], ["-", 0, 14, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 1, 0, 27, 17, 29]]
| 1
| 273
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
using namespace std;
string s, t;
int a, p;
int A(int x) { return x == 2 ? 0 : x == 1 ? 1 : 2; }
int B(int x, int y) { return x == 0 || y == 0 ? 0 : x == 1 || y == 1 ? 1 : 2; }
int C(int x, int y) { return x == 2 || y == 2 ? 2 : x == 1 || y == 1 ? 1 : 0; }
int E() {
while (s[p] == ')')
p++;
if (s[p] != '(') {
int q = p;
while (s[p] == '-')
p++;
q = (p - q) % 2;
if (s[p] == '(')
q ? A(E()) : E();
else
return q ? A(s[p] - '0') : s[p] - '0';
}
int x, y;
char z;
p++;
x = E();
p++;
z = s[p];
p++;
y = E();
p++;
return z != '+' ? B(x, y) : C(x, y);
}
int main() {
while (cin >> t, t != ".") {
a = 0;
rep(i, 3) rep(j, 3) rep(k, 3) {
p = 0;
s = t;
rep(v, s.size()) s[v] = s[v] == 'P' ? '0' + i
: s[v] == 'Q' ? '0' + j
: s[v] == 'R' ? '0' + k
: s[v];
if (E() == 2)
a++;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
using namespace std;
string s, t;
int a, p;
int A(int x) { return x == 2 ? 0 : x == 1 ? 1 : 2; }
int B(int x, int y) { return x == 0 || y == 0 ? 0 : x == 1 || y == 1 ? 1 : 2; }
int C(int x, int y) { return x == 2 || y == 2 ? 2 : x == 1 || y == 1 ? 1 : 0; }
int E() {
while (s[p] == ')')
p++;
if (s[p] != '(') {
int q = p;
while (s[p] == '-')
p++;
q = (p - q) % 2;
if (s[p] == '(')
return q ? A(E()) : E();
else
return q ? A(s[p] - '0') : s[p] - '0';
}
int x, y;
char z;
p++;
x = E();
p++;
z = s[p];
p++;
y = E();
p++;
return z != '+' ? B(x, y) : C(x, y);
}
int main() {
while (cin >> t, t != ".") {
a = 0;
rep(i, 3) rep(j, 3) rep(k, 3) {
p = 0;
s = t;
rep(v, s.size()) s[v] = s[v] == 'P' ? '0' + i
: s[v] == 'Q' ? '0' + j
: s[v] == 'R' ? '0' + k
: s[v];
if (E() == 2)
a++;
}
cout << a << endl;
}
}
|
[["+", 0, 57, 64, 9, 0, 57, 64, 37, 0, 38]]
| 1
| 426
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
int expr(string &s, int &i);
int term(string &s, int &i);
int fact(string &s, int &i);
int numb(string &s, int &i);
int expr(string &s, int &i) {
int val = term(s, i);
while (s[i] == '+') {
i++;
val = max(val, term(s, i));
}
return val;
}
int term(string &s, int &i) {
int val = fact(s, i);
while (s[i] == '*') {
i++;
int x = fact(s, i);
if (min(x, val) == 0)
val = 0;
else if (min(x, val) == 1)
val = 1;
else
val = 2;
}
// cout << val << endl;
return val;
}
int invert(string &s, int &i) {
if (isdigit(s[i]))
return fact(s, i);
// cout << i << endl;
while (s[i] == '-') {
i++;
int val2 = invert(s, i);
if (val2 == 2)
return 0;
else if (val2 == 0)
return 2;
return 1;
}
return 0;
}
int fact(string &s, int &i) {
if (s[i] == '-')
return invert(s, i);
if (isdigit(s[i]))
return numb(s, i);
i++;
int ret = expr(s, i);
i++;
return ret;
}
int numb(string &s, int &i) {
int n = s[i++] - '0';
while (isdigit(s[i]))
n = n * 10 + s[i++] - '0';
return n;
}
int main() {
string S;
while (cin >> S, S != ".") {
int res = 0;
rep(i, 3) rep(j, 3) rep(k, 3) {
string s = S;
rep(l, s.size()) {
if (s[l] == 'P')
s[l] = i + '0';
if (s[l] == 'Q')
s[l] = j + '0';
if (s[l] == 'R')
s[l] = k + '0';
}
int l = 0;
if (expr(s, l) == 2)
res++;
}
cout << res << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;
int expr(string &s, int &i);
int term(string &s, int &i);
int fact(string &s, int &i);
int numb(string &s, int &i);
int expr(string &s, int &i) {
int val = term(s, i);
while (s[i] == '+') {
i++;
val = max(val, term(s, i));
}
return val;
}
int term(string &s, int &i) {
int val = fact(s, i);
while (s[i] == '*') {
i++;
int x = fact(s, i);
if (min(x, val) == 0)
val = 0;
else if (min(x, val) == 1)
val = 1;
else
val = 2;
}
// cout << val << endl;
return val;
}
int invert(string &s, int &i) {
if (isdigit(s[i]))
return fact(s, i);
while (s[i] == '-') {
i++;
int val2 = fact(s, i);
if (val2 == 2)
return 0;
else if (val2 == 0)
return 2;
return 1;
}
return 0;
}
int fact(string &s, int &i) {
if (s[i] == '-')
return invert(s, i);
if (isdigit(s[i]))
return numb(s, i);
i++;
int ret = expr(s, i);
i++;
return ret;
}
int numb(string &s, int &i) {
int n = s[i++] - '0';
while (isdigit(s[i]))
n = n * 10 + s[i++] - '0';
return n;
}
int main() {
string S;
while (cin >> S, S != ".") {
int res = 0;
rep(i, 3) rep(j, 3) rep(k, 3) {
string s = S;
rep(l, s.size()) {
if (s[l] == 'P')
s[l] = i + '0';
if (s[l] == 'Q')
s[l] = j + '0';
if (s[l] == 'R')
s[l] = k + '0';
}
int l = 0;
if (expr(s, l) == 2)
res++;
}
cout << res << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
| 1
| 721
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T;
int pos;
int solve() {
if (T[pos] == '0') {
pos++;
return 0;
}
if (T[pos] == '1') {
pos++;
return 1;
}
if (T[pos] == '2') {
pos++;
return 2;
}
if (T[pos] == '-') {
pos++;
return 2 - solve();
}
if (T[pos] == '(') {
pos++;
int a = solve();
char c = T[pos];
pos++;
int b = solve();
if (c == '+')
return max(a, b);
if (c == '*')
return min(a, b);
}
return 0;
}
int main() {
while (true) {
cin >> S;
if (S == ".")
break;
int ret = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
T = S;
for (int l = 0; l < S.size(); l++) {
if (S[l] == 'P')
T[l] = i + 48;
if (S[l] == 'Q')
T[l] = j + 48;
if (S[l] == 'R')
T[l] = k + 48;
}
pos = 0;
if (solve() == 2) {
ret++;
}
}
}
}
printf("%d\n", ret);
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T;
int pos;
int solve() {
if (T[pos] == '0') {
pos++;
return 0;
}
if (T[pos] == '1') {
pos++;
return 1;
}
if (T[pos] == '2') {
pos++;
return 2;
}
if (T[pos] == '-') {
pos++;
return 2 - solve();
}
if (T[pos] == '(') {
pos++;
int a = solve();
char c = T[pos];
pos++;
int b = solve();
pos++;
if (c == '+')
return max(a, b);
if (c == '*')
return min(a, b);
}
return 0;
}
int main() {
while (true) {
cin >> S;
if (S == ".")
break;
int ret = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
T = S;
for (int l = 0; l < S.size(); l++) {
if (S[l] == 'P')
T[l] = i + 48;
if (S[l] == 'Q')
T[l] = j + 48;
if (S[l] == 'R')
T[l] = k + 48;
}
pos = 0;
if (solve() == 2) {
ret++;
}
}
}
}
printf("%d\n", ret);
}
return 0;
}
|
[["+", 0, 57, 64, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 27, 17, 29], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
| 1
| 373
|
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;
void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template <class T, class U> void reader(T &t, U &u) {
reader(t);
reader(u);
}
template <class T, class U, class V> void reader(T &t, U &u, V &v) {
reader(t);
reader(u);
reader(v);
}
void writer(const int a, char c) {
printf("%d", a);
putchar(c);
}
void writer(const ll a, char c) {
printf("%lld", a);
putchar(c);
}
void writer(const ull a, char c) {
printf("%llu", a);
putchar(c);
}
void writer(const double a, char c) {
printf("%.20lf", a);
putchar(c);
}
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) {
printf("%s", a);
putchar(c);
};
void writer(const char a, char c) {
putchar(a);
putchar(c);
};
template <class T> void writerLn(T t) { writer(t, '\n'); }
template <class T, class U> void writerLn(T t, U u) {
writer(t, ' ');
writer(u, '\n');
}
template <class T, class U, class V> void writerLn(T t, U u, V v) {
writer(t, ' ');
writer(u, ' ');
writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
int i;
if (!n) {
putchar('\n');
return;
}
rep(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
template <class T> void writerVec(vector<T> x) {
int n = x.size();
int i;
if (!n) {
putchar('\n');
return;
}
rep(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
vector<string> split(const string &str, char sep) {
vector<string> v;
stringstream ss(str);
string buffer;
while (getline(ss, buffer, sep)) {
v.push_back(buffer);
}
return v;
}
// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX 200005
int N, M;
int dp[MAX];
bool taked[MAX];
// expr +
// term *
int P, Q, R;
int number(string &s, int &i);
int factor(string &s, int &i);
int not_calc(string &s, int &i);
int term(string &s, int &i);
int expr(string &s, int &i);
int number(string &s, int &i) {
int res;
switch (s[i]) {
case 'P':
res = P;
break;
case 'Q':
res = Q;
break;
case 'R':
res = R;
break;
default:
res = s[i] - '0';
}
i++;
if (test) {
dump(i);
fprintf(stderr, "number ");
dump(res);
}
return res;
}
int factor(string &s, int &i) {
if (s[i] != '(')
return number(s, i);
i++;
int ret = expr(s, i);
i++;
if (test) {
fprintf(stderr, "factor : ");
dump(ret);
}
return ret;
}
int not_calc(string &s, int &i) {
int val;
int cnt = 0;
while (s[i] == '-') {
cnt++;
i++;
}
val = factor(s, i);
if (cnt % 2) {
val = 2 - val;
}
if (test) {
fprintf(stderr, "not_calc ");
dump(val);
}
return val;
}
int term(string &s, int &i) {
int val = not_calc(s, i);
while (s[i] == '*') {
i++;
int val2 = not_calc(s, i);
if (val == 0 || val2 == 0)
val = 0;
else if (val == 1 || val2 == 1)
val = 1;
}
if (test) {
fprintf(stderr, "term ");
dump(val);
}
return val;
}
int expr(string &s, int &i) {
int val = term(s, i);
while (s[i] == '+') {
i++;
int val2 = term(s, i);
if (val == 2 || val2 == 2)
val = 2;
else if (val == 1 || val == 1)
val = 1;
}
if (test) {
fprintf(stderr, "sum ");
dump(val);
}
return val;
}
signed main(void) {
int i, j, k;
while (1) {
string s;
int cnt = 0;
cin >> s;
if (s == ".")
return 0;
// i = 0;
// P = 0; Q = 0; R = 0;
// expr(s, i);
rep(P, 3) rep(Q, 3) rep(R, 3) {
// dump(P);
// dump(Q);
// dump(R);
i = 0;
if (expr(s, i) == 2)
cnt++;
}
cout << cnt << endl;
}
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define ALL(a) (a).begin(), (a).end()
#define ifnot(a) if (not a)
#define dump(x) cerr << #x << " = " << (x) << endl
using namespace std;
void reader(int &a) { scanf("%d", &a); }
void reader(double &a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char &a) { scanf(" %c", &a); }
void reader(ll &a) { scanf("%lld", &a); }
void reader(ull &a) { scanf("%llu", &a); }
// void reader(string& a){cin >> a;};
template <class T, class U> void reader(T &t, U &u) {
reader(t);
reader(u);
}
template <class T, class U, class V> void reader(T &t, U &u, V &v) {
reader(t);
reader(u);
reader(v);
}
void writer(const int a, char c) {
printf("%d", a);
putchar(c);
}
void writer(const ll a, char c) {
printf("%lld", a);
putchar(c);
}
void writer(const ull a, char c) {
printf("%llu", a);
putchar(c);
}
void writer(const double a, char c) {
printf("%.20lf", a);
putchar(c);
}
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) {
printf("%s", a);
putchar(c);
};
void writer(const char a, char c) {
putchar(a);
putchar(c);
};
template <class T> void writerLn(T t) { writer(t, '\n'); }
template <class T, class U> void writerLn(T t, U u) {
writer(t, ' ');
writer(u, '\n');
}
template <class T, class U, class V> void writerLn(T t, U u, V v) {
writer(t, ' ');
writer(u, ' ');
writer(v, '\n');
}
template <class T> void writerArr(T x[], int n) {
int i;
if (!n) {
putchar('\n');
return;
}
rep(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
template <class T> void writerVec(vector<T> x) {
int n = x.size();
int i;
if (!n) {
putchar('\n');
return;
}
rep(i, n - 1) writer(x[i], ' ');
writer(x[n - 1], '\n');
}
vector<string> split(const string &str, char sep) {
vector<string> v;
stringstream ss(str);
string buffer;
while (getline(ss, buffer, sep)) {
v.push_back(buffer);
}
return v;
}
// #define int ll
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
#define MAX 200005
int N, M;
int dp[MAX];
bool taked[MAX];
// expr +
// term *
int P, Q, R;
int number(string &s, int &i);
int factor(string &s, int &i);
int not_calc(string &s, int &i);
int term(string &s, int &i);
int expr(string &s, int &i);
int number(string &s, int &i) {
int res;
switch (s[i]) {
case 'P':
res = P;
break;
case 'Q':
res = Q;
break;
case 'R':
res = R;
break;
default:
res = s[i] - '0';
}
i++;
if (test) {
dump(i);
fprintf(stderr, "number ");
dump(res);
}
return res;
}
int factor(string &s, int &i) {
if (s[i] != '(')
return number(s, i);
i++;
int ret = expr(s, i);
i++;
if (test) {
fprintf(stderr, "factor : ");
dump(ret);
}
return ret;
}
int not_calc(string &s, int &i) {
int val;
int cnt = 0;
while (s[i] == '-') {
cnt++;
i++;
}
val = factor(s, i);
if (cnt % 2) {
val = 2 - val;
}
if (test) {
fprintf(stderr, "not_calc ");
dump(val);
}
return val;
}
int term(string &s, int &i) {
int val = not_calc(s, i);
while (s[i] == '*') {
i++;
int val2 = not_calc(s, i);
if (val == 0 || val2 == 0)
val = 0;
else if (val == 1 || val2 == 1)
val = 1;
}
if (test) {
fprintf(stderr, "term ");
dump(val);
}
return val;
}
int expr(string &s, int &i) {
int val = term(s, i);
while (s[i] == '+') {
i++;
int val2 = term(s, i);
if (val == 2 || val2 == 2)
val = 2;
else if (val == 1 || val2 == 1)
val = 1;
}
if (test) {
fprintf(stderr, "sum ");
dump(val);
}
return val;
}
signed main(void) {
int i, j, k;
while (1) {
string s;
int cnt = 0;
cin >> s;
if (s == ".")
return 0;
// i = 0;
// P = 0; Q = 0; R = 0;
// expr(s, i);
rep(P, 3) rep(Q, 3) rep(R, 3) {
// dump(P);
// dump(Q);
// dump(R);
i = 0;
if (expr(s, i) == 2)
cnt++;
}
cout << cnt << endl;
}
return 0;
}
|
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22]]
| 1
| 1,440
|
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s;
int p, q, r, i;
int dfs() {
int t[2] = {}, c = 0, m = 0, w = 0, k = 0, a = 0, pp = 0;
t[0] = t[1] = -100;
if (s[i] == '(')
i++;
for (; i < s.size(); i++) {
pp = 0;
if (s[i] == '(')
t[c++] = dfs(), a++, pp++;
else if (s[i] >= '0' && s[i] <= '2')
t[c++] = s[i] - '0', a++;
else if (s[i] == 'P')
t[c++] = p, a++;
else if (s[i] == 'Q')
t[c++] = q, a++;
else if (s[i] == 'R')
t[c++] = r, a++;
else if (s[i] == '-')
m++;
else if (s[i] == '+')
w++, a = 0;
else if (s[i] == '*')
k++, a = 0;
if (m % 2 && a && !pp) {
t[c - 1] = abs(t[c - 1] - 2);
a = m = 0;
}
if (s[i] == ')' && !pp) {
if (c == 1)
return t[0];
if (w)
return max(t[0], t[1]);
if (k)
return min(t[0], t[1]);
}
}
return t[0];
}
int main() {
while (cin >> s, s != ".") {
int res = 0;
r(x, 3) r(y, 3) r(z, 3) {
p = x;
q = y;
r = z;
i = 0;
if (dfs() == 2)
res++;
}
cout << res << endl;
}
}
|
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string s;
int p, q, r, i;
int dfs() {
int t[2] = {}, c = 0, m = 0, w = 0, k = 0, a = 0, pp = 0;
t[0] = t[1] = -100;
if (s[i] == '(')
i++;
for (; i < s.size(); i++) {
pp = 0;
if (s[i] == '(')
t[c++] = dfs(), a++, pp++;
else if (s[i] >= '0' && s[i] <= '2')
t[c++] = s[i] - '0', a++;
else if (s[i] == 'P')
t[c++] = p, a++;
else if (s[i] == 'Q')
t[c++] = q, a++;
else if (s[i] == 'R')
t[c++] = r, a++;
else if (s[i] == '-')
m++;
else if (s[i] == '+')
w++, a = 0;
else if (s[i] == '*')
k++, a = 0;
if (m % 2 && a) {
t[c - 1] = abs(t[c - 1] - 2);
a = m = 0;
}
if (s[i] == ')' && !pp) {
if (c == 1)
return t[0];
if (w)
return max(t[0], t[1]);
if (k)
return min(t[0], t[1]);
}
}
return t[0];
}
int main() {
while (cin >> s, s != ".") {
int res = 0;
r(x, 3) r(y, 3) r(z, 3) {
p = x;
q = y;
r = z;
i = 0;
if (dfs() == 2)
res++;
}
cout << res << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["-", 0, 57, 15, 339, 51, 16, 12, 91, 17, 111], ["-", 0, 57, 15, 339, 51, 16, 12, 91, 28, 22]]
| 1
| 488
|
#include <bits/stdc++.h>
using namespace std;
#define State string::const_iterator
class ParseError {};
int expression(State &begin);
// int term(State &begin);
int number(State &begin);
int factor(State &begin);
int denial(State &begin);
void consume(State &begin, char expected);
int p, q, r;
string input;
// 四則演算の式をパースして、その評価結果を返す。
int expression(State &begin) {
int ret = factor(begin);
while (true) {
if (*begin == '+') {
consume(begin, '+');
int num = factor(begin);
// cout << num << " + " << ret;
if (ret == 0 && num == 0) {
ret = 0;
} else if (ret == 1 || num == 1) {
if (ret != 2 && num != 2) {
ret = 1;
} else {
ret = 2;
}
} else {
ret = 2;
}
// cout << " = " << ret << endl;
} else if (*begin == '*') {
consume(begin, '*');
int num = factor(begin);
// cout << num << " * " << ret;
if (num == 2 && ret == 2) {
ret = 2;
} else if (num == 0 || ret == 0) {
ret = 0;
} else {
ret = 1;
}
// cout << " = " << ret << endl;
} else {
break;
}
}
return ret;
}
int denial(State &begin) {
if (*begin == '-') {
consume(begin, '-');
int ret = denial(begin);
if (ret == 2) {
ret = 0;
} else if (ret == 0) {
ret = 2;
}
return ret;
}
return number(begin);
}
// 数字の列をパースして、その数を返す。
int number(State &begin) {
if (*begin == 'P') {
consume(begin, 'P');
return p;
} else if (*begin == 'Q') {
consume(begin, 'Q');
return q;
} else if (*begin == 'R') {
consume(begin, 'R');
return r;
}
int ret = 0;
while (isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
// 括弧か数をパースして、その評価結果を返す。
int factor(State &begin) {
if (*begin == '(') {
consume(begin, '('); // '('を飛ばす。
int ret = expression(begin);
consume(begin, ')'); // ')'を飛ばす。
return ret;
} else {
return denial(begin);
}
}
// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
if (*begin == expected) {
begin++;
} else {
cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
cerr << "Rest string is '";
while (*begin) {
cerr << *begin++;
}
cerr << "'" << endl;
cerr << input.end() - begin << endl;
throw ParseError();
}
}
int main() {
while (true) {
cin >> input;
if (input[0] == '.') {
break;
}
int ans = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
p = i;
q = j;
r = k;
State s = input.begin();
if (factor(s) == 2) {
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define State string::const_iterator
class ParseError {};
int expression(State &begin);
// int term(State &begin);
int number(State &begin);
int factor(State &begin);
int denial(State &begin);
void consume(State &begin, char expected);
int p, q, r;
string input;
// 四則演算の式をパースして、その評価結果を返す。
int expression(State &begin) {
int ret = factor(begin);
while (true) {
if (*begin == '+') {
consume(begin, '+');
int num = factor(begin);
// cout << num << " + " << ret;
if (ret == 0 && num == 0) {
ret = 0;
} else if (ret == 1 || num == 1) {
if (ret != 2 && num != 2) {
ret = 1;
} else {
ret = 2;
}
} else {
ret = 2;
}
// cout << " = " << ret << endl;
} else if (*begin == '*') {
consume(begin, '*');
int num = factor(begin);
// cout << num << " * " << ret;
if (num == 2 && ret == 2) {
ret = 2;
} else if (num == 0 || ret == 0) {
ret = 0;
} else {
ret = 1;
}
// cout << " = " << ret << endl;
} else {
break;
}
}
return ret;
}
int denial(State &begin) {
if (*begin == '-') {
consume(begin, '-');
int ret = factor(begin);
if (ret == 2) {
ret = 0;
} else if (ret == 0) {
ret = 2;
}
return ret;
}
return number(begin);
}
// 数字の列をパースして、その数を返す。
int number(State &begin) {
if (*begin == 'P') {
consume(begin, 'P');
return p;
} else if (*begin == 'Q') {
consume(begin, 'Q');
return q;
} else if (*begin == 'R') {
consume(begin, 'R');
return r;
}
int ret = 0;
while (isdigit(*begin)) {
ret *= 10;
ret += *begin - '0';
begin++;
}
return ret;
}
// 括弧か数をパースして、その評価結果を返す。
int factor(State &begin) {
if (*begin == '(') {
consume(begin, '('); // '('を飛ばす。
int ret = expression(begin);
consume(begin, ')'); // ')'を飛ばす。
return ret;
} else {
return denial(begin);
}
}
// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
if (*begin == expected) {
begin++;
} else {
cerr << "Expected '" << expected << "' but got '" << *begin << "'" << endl;
cerr << "Rest string is '";
while (*begin) {
cerr << *begin++;
}
cerr << "'" << endl;
cerr << input.end() - begin << endl;
throw ParseError();
}
}
int main() {
while (true) {
cin >> input;
if (input[0] == '.') {
break;
}
int ans = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
p = i;
q = j;
r = k;
State s = input.begin();
if (factor(s) == 2) {
ans++;
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
[["-", 64, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 64, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
| 1
| 728
|
#include <iostream>
#include <string>
using namespace std;
#define N_MAX 80
string s, s_copy;
int ans = 0;
int point = 0;
char ch(int i) {
if (i == 0)
return '0';
else if (i == 1)
return '1';
else
return '2';
}
void set(int i, int j, int z) {
s_copy = s;
int c = s.size();
for (int k = 0; k < c; k++) {
if (s_copy[k] == 'P')
s_copy[k] = ch(i);
if (s_copy[k] == 'Q')
s_copy[k] = ch(j);
if (s_copy[k] == 'R')
s_copy[k] = ch(z);
}
}
int mainus(int i) {
switch (i) {
case 0:
return 2;
case 1:
return 1;
case 2:
return 0;
}
}
int kakeru(int x, int y) { return x > y ? y : x; }
int tasu(int x, int y) { return x > y ? x : y; }
int calc() {
switch (s_copy[point++]) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '-':
return mainus(calc());
case '(':
int left = calc();
char temp = s_copy[point++];
int right = calc();
if (temp == '+')
return tasu(left, right);
else
return kakeru(left, right);
}
}
int main() {
while (true) {
cin >> s;
if (s == ".")
break;
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++)
for (int z = 0; z <= 2; z++) {
set(i, j, z);
point = 0;
if (calc() == 2)
ans++;
}
cout << ans << "\n";
ans = 0;
}
return 0;
}
|
#include <iostream>
#include <string>
using namespace std;
#define N_MAX 80
string s, s_copy;
int ans = 0;
int point = 0;
char ch(int i) {
if (i == 0)
return '0';
else if (i == 1)
return '1';
else
return '2';
}
void set(int i, int j, int z) {
s_copy = s;
int c = s.size();
for (int k = 0; k < c; k++) {
if (s_copy[k] == 'P')
s_copy[k] = ch(i);
if (s_copy[k] == 'Q')
s_copy[k] = ch(j);
if (s_copy[k] == 'R')
s_copy[k] = ch(z);
}
}
int mainus(int i) {
switch (i) {
case 0:
return 2;
case 1:
return 1;
case 2:
return 0;
}
}
int kakeru(int x, int y) { return x > y ? y : x; }
int tasu(int x, int y) { return x > y ? x : y; }
int calc() {
switch (s_copy[point++]) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '-':
return mainus(calc());
case '(':
int left = calc();
char temp = s_copy[point++];
int right = calc();
point++;
if (temp == '+')
return tasu(left, right);
else
return kakeru(left, right);
}
}
int main() {
while (true) {
cin >> s;
if (s == ".")
break;
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++)
for (int z = 0; z <= 2; z++) {
set(i, j, z);
point = 0;
if (calc() == 2)
ans++;
}
cout << ans << "\n";
ans = 0;
}
return 0;
}
|
[["+", 8, 9, 0, 100, 0, 1, 0, 27, 28, 22], ["+", 8, 9, 0, 100, 0, 1, 0, 27, 17, 29], ["+", 0, 99, 8, 9, 0, 100, 0, 1, 0, 35]]
| 1
| 456
|
#include <cassert>
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;
string str;
int P, Q, R;
int Minus(int a) { return 2 - a; }
int Prod(int a, int b) {
if (a == 0) {
return 0;
}
if (b == 0) {
return 0;
}
if (a == 1) {
return 1;
}
if (b == 1) {
return 1;
}
if (a == 2) {
return 2;
}
}
int Sum(int a, int b) { return 2 - Prod(2 - a, 2 - b); }
/*
int Minus[] = {2, 1, 0};
int Prod[][3] = {
{0, 0, 0},
{0, 1, 1},
{0, 1, 2}};
int Sum[][3] = {
{0, 1, 2},
{1, 1, 2},
{2, 2, 2}};
*/
typedef pair<int, int> Res;
Res formula(int i) {
if (str[i] == '-') {
Res res = formula(i + 1);
return Res(Minus(res.first), res.second);
} else if (str[i] == '(') {
Res lhs = formula(i + 1);
i = lhs.second;
if (str[i] == '+') {
Res rhs = formula(i + 1);
assert(str[rhs.second] == ')');
return Res(Sum(lhs.first, rhs.first), rhs.second + 1);
} else if (str[i] == '*') {
Res rhs = formula(i + 1);
assert(str[rhs.second] == ')');
return Res(Prod(lhs.first, rhs.first), rhs.second + 1);
}
assert(false);
} else if (str[i] == 'P') {
return Res(P, i + 1);
} else if (str[i] == 'Q') {
return Res(Q, i + 1);
} else if (str[i] == 'R') {
return Res(R, i + 1);
} else if (str[i] == '0') {
return Res(0, i + 1);
} else if (str[i] == '1') {
return Res(1, i + 1);
} else if (str[i] == '2') {
return Res(2, i + 1);
}
assert(false);
}
void solve() {
int cnt;
for (P = 0; P < 3; P++) {
for (Q = 0; Q < 3; Q++) {
for (R = 0; R < 3; R++) {
Res res = formula(0);
// assert(res.second == str.size());
if (res.first == 2) {
cnt++;
}
}
}
}
cout << cnt << endl;
}
int main() {
while (true) {
getline(cin, str);
if (str == ".") {
return 0;
}
solve();
}
}
|
#include <cassert>
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;
string str;
int P, Q, R;
int Minus(int a) { return 2 - a; }
int Prod(int a, int b) {
if (a == 0) {
return 0;
}
if (b == 0) {
return 0;
}
if (a == 1) {
return 1;
}
if (b == 1) {
return 1;
}
if (a == 2) {
return 2;
}
}
int Sum(int a, int b) { return 2 - Prod(2 - a, 2 - b); }
/*
int Minus[] = {2, 1, 0};
int Prod[][3] = {
{0, 0, 0},
{0, 1, 1},
{0, 1, 2}};
int Sum[][3] = {
{0, 1, 2},
{1, 1, 2},
{2, 2, 2}};
*/
typedef pair<int, int> Res;
Res formula(int i) {
if (str[i] == '-') {
Res res = formula(i + 1);
return Res(Minus(res.first), res.second);
} else if (str[i] == '(') {
Res lhs = formula(i + 1);
i = lhs.second;
if (str[i] == '+') {
Res rhs = formula(i + 1);
assert(str[rhs.second] == ')');
return Res(Sum(lhs.first, rhs.first), rhs.second + 1);
} else if (str[i] == '*') {
Res rhs = formula(i + 1);
assert(str[rhs.second] == ')');
return Res(Prod(lhs.first, rhs.first), rhs.second + 1);
}
assert(false);
} else if (str[i] == 'P') {
return Res(P, i + 1);
} else if (str[i] == 'Q') {
return Res(Q, i + 1);
} else if (str[i] == 'R') {
return Res(R, i + 1);
} else if (str[i] == '0') {
return Res(0, i + 1);
} else if (str[i] == '1') {
return Res(1, i + 1);
} else if (str[i] == '2') {
return Res(2, i + 1);
}
assert(false);
}
void solve() {
int cnt = 0;
for (P = 0; P < 3; P++) {
for (Q = 0; Q < 3; Q++) {
for (R = 0; R < 3; R++) {
Res res = formula(0);
// assert(res.second == str.size());
if (res.first == 2) {
cnt++;
}
}
}
}
cout << cnt << endl;
}
int main() {
while (true) {
getline(cin, str);
if (str == ".") {
return 0;
}
solve();
}
}
|
[["+", 0, 14, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 601
|
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1155&lang=jp
sorce: ICPC Domestic 2008
構文解析の問題。
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
下のように書き換え
<expr>::=<term1><op><term1>|<term1>
<term1>::='-'<term1>|<term2>
<term2>::='('<expr>')'|0|1|2
文字の部分は数字に置き換え、全探索。
*/
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int _and[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
const int _or[3][3] = {{0, 1, 2}, {0, 1, 2}, {2, 2, 2}};
const int _not[3] = {2, 1, 0};
string s;
int idx;
int term1(void);
int term2(void);
int expr(void);
int term1(void) {
if (s[idx] == '-') {
idx++;
return _not[term1()];
} else
return term2();
}
int term2(void) {
int res = 0;
if (s[idx] == '(') {
idx++;
res = expr();
idx++;
} else {
return (s[idx] - '0');
} // end if
return res;
}
int expr(void) {
int a = term1();
while (idx < s.size() && s[idx] != ')') {
char op = s[idx++];
int b = term1();
switch (op) {
case '*':
a = _and[a][b];
break;
case '+':
a = _or[a][b];
break;
} // end switch
} // end if
return a;
}
int parse(void) {
idx = 0;
int res = expr();
return res;
}
int main() {
ios_base::sync_with_stdio(0);
string str = "";
while (cin >> str, str != ".") {
int res = 0;
int n = str.length();
rep(p, 3) {
string s1 = str;
rep(i, n) if (s1[i] == 'P') s1[i] = (char)('0' + p);
rep(q, 3) {
string t1 = s1;
rep(i, n) if (t1[i] == 'Q') t1[i] = (char)('0' + q);
rep(r, 3) {
s = t1;
rep(i, n) if (s[i] == 'R') s[i] = (char)('0' + r);
int ans = parse();
// cerr << s << '=' << ans << endl;
if (ans == 2)
res++;
} // end rep
} // end rep
} // end rep
cout << res << endl;
} // end while
return 0;
}
|
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1155&lang=jp
sorce: ICPC Domestic 2008
構文解析の問題。
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
下のように書き換え
<expr>::=<term1><op><term1>|<term1>
<term1>::='-'<term1>|<term2>
<term2>::='('<expr>')'|0|1|2
文字の部分は数字に置き換え、全探索。
*/
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int _and[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
const int _or[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
const int _not[3] = {2, 1, 0};
string s;
int idx;
int term1(void);
int term2(void);
int expr(void);
int term1(void) {
if (s[idx] == '-') {
idx++;
return _not[term1()];
} else
return term2();
}
int term2(void) {
int res = 0;
if (s[idx] == '(') {
idx++;
res = expr();
idx++;
} else {
return (s[idx] - '0');
} // end if
return res;
}
int expr(void) {
int a = term1();
while (idx < s.size() && s[idx] != ')') {
char op = s[idx++];
int b = term1();
switch (op) {
case '*':
a = _and[a][b];
break;
case '+':
a = _or[a][b];
break;
} // end switch
} // end if
return a;
}
int parse(void) {
idx = 0;
int res = expr();
return res;
}
int main() {
ios_base::sync_with_stdio(0);
string str = "";
while (cin >> str, str != ".") {
int res = 0;
int n = str.length();
rep(p, 3) {
string s1 = str;
rep(i, n) if (s1[i] == 'P') s1[i] = (char)('0' + p);
rep(q, 3) {
string t1 = s1;
rep(i, n) if (t1[i] == 'Q') t1[i] = (char)('0' + q);
rep(r, 3) {
s = t1;
rep(i, n) if (s[i] == 'R') s[i] = (char)('0' + r);
int ans = parse();
// cerr << s << '=' << ans << endl;
if (ans == 2)
res++;
} // end rep
} // end rep
} // end rep
cout << res << endl;
} // end while
return 0;
}
|
[["-", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13], ["+", 0, 43, 49, 50, 51, 83, 0, 83, 0, 13]]
| 1
| 631
|
#include <bits/stdc++.h>
using namespace std;
#define EPS (1e-10)
typedef complex<double> P;
//実数同士の比較
bool EQ(double a, double b) { return (abs(a - b) < EPS); }
//二つのベクトルが等しいかどうか
bool EQV(P a, P b) {
return (EQ(a.real(), b.real()) && EQ(a.imag(), b.imag()));
}
//内積
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
//外積
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// 点a,bを通る直線と点cとの距離
double distance_l_p(P a, P b, P c) {
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点cが線分a,b上にあるかないか(3)
int is_point_on_line(P a, P b, P c) {
// |a-c| + |c-b| <= |a-b| なら線分上
return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(P a, P b, P c) {
if (dot(b - a, c - a) < EPS)
return abs(c - a);
if (dot(a - b, c - b) < EPS)
return abs(c - b);
return abs(cross(b - a, c - a)) / abs(b - a);
}
//図を思い出す
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return +1;
if (cross(b, c) < -EPS)
return -1;
if (dot(b, c) < 0)
return +2;
if (norm(b) < norm(c))
return -2;
return 0;
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_ls(P a1, P a2, P b1, P b2) {
return (ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0) &&
(ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0);
}
// a1,a2を通る直線とb1,b2を通る直線の交点計算
P intersection_l(P a1, P a2, P b1, P b2) {
P a = a2 - a1;
P b = b2 - b1;
return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
double distance(P p0, P p1, P p2, P p3) {
double ans = 1e10;
ans = min(ans, distance_ls_p(p0, p1, p2));
ans = min(ans, distance_ls_p(p0, p1, p3));
ans = min(ans, distance_ls_p(p2, p3, p0));
ans = min(ans, distance_ls_p(p2, p3, p1));
if (is_intersected_ls(p0, p1, p2, p3))
ans = 0.0;
return ans;
}
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
double x, y;
cin >> x >> y;
P s = P(x, y);
cin >> x >> y;
P g = P(x, y);
vector<P> mins(n), maxs(n);
vector<double> h(n);
for (int i = 0; i < n; i++) {
cin >> x >> y;
mins[i].real(x);
mins[i].imag(y);
cin >> x >> y;
maxs[i].real(x);
maxs[i].imag(y);
cin >> h[i];
}
double ans = 1e9;
for (int i = 0; i < n; i++) {
double d = 1e9;
P p1 = mins[i], p3 = maxs[i];
P p2(mins[i].real(), maxs[i].imag());
P p4(maxs[i].real(), mins[i].imag());
if (p1.real() < s.real() && s.real() < p3.real() &&
p1.imag() < s.imag() && s.imag() < p3.real()) {
ans = 0;
break;
}
d = min(d, distance(s, g, p1, p2));
d = min(d, distance(s, g, p2, p3));
d = min(d, distance(s, g, p3, p4));
d = min(d, distance(s, g, p4, p1));
double r = (h[i] * h[i] + d * d) / (2 * h[i]);
if (h[i] < d) {
ans = min(ans, r);
} else {
ans = min(ans, d);
}
}
printf("%f\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define EPS (1e-10)
typedef complex<double> P;
//実数同士の比較
bool EQ(double a, double b) { return (abs(a - b) < EPS); }
//二つのベクトルが等しいかどうか
bool EQV(P a, P b) {
return (EQ(a.real(), b.real()) && EQ(a.imag(), b.imag()));
}
//内積
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
//外積
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// 点a,bを通る直線と点cとの距離
double distance_l_p(P a, P b, P c) {
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点cが線分a,b上にあるかないか(3)
int is_point_on_line(P a, P b, P c) {
// |a-c| + |c-b| <= |a-b| なら線分上
return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(P a, P b, P c) {
if (dot(b - a, c - a) < EPS)
return abs(c - a);
if (dot(a - b, c - b) < EPS)
return abs(c - b);
return abs(cross(b - a, c - a)) / abs(b - a);
}
//図を思い出す
int ccw(P a, P b, P c) {
b -= a;
c -= a;
if (cross(b, c) > EPS)
return +1;
if (cross(b, c) < -EPS)
return -1;
if (dot(b, c) < 0)
return +2;
if (norm(b) < norm(c))
return -2;
return 0;
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_ls(P a1, P a2, P b1, P b2) {
return (ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0) &&
(ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0);
}
// a1,a2を通る直線とb1,b2を通る直線の交点計算
P intersection_l(P a1, P a2, P b1, P b2) {
P a = a2 - a1;
P b = b2 - b1;
return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
double distance(P p0, P p1, P p2, P p3) {
double ans = 1e10;
ans = min(ans, distance_ls_p(p0, p1, p2));
ans = min(ans, distance_ls_p(p0, p1, p3));
ans = min(ans, distance_ls_p(p2, p3, p0));
ans = min(ans, distance_ls_p(p2, p3, p1));
if (is_intersected_ls(p0, p1, p2, p3))
ans = 0.0;
return ans;
}
int main() {
while (true) {
int n;
cin >> n;
if (n == 0)
break;
double x, y;
cin >> x >> y;
P s = P(x, y);
cin >> x >> y;
P g = P(x, y);
vector<P> mins(n), maxs(n);
vector<double> h(n);
for (int i = 0; i < n; i++) {
cin >> x >> y;
// if(z == 24)printf("%.0f %.0f",x,y);
mins[i].real(x);
mins[i].imag(y);
cin >> x >> y;
// if(z == 24)printf(" %.0f %.0f",x,y);
maxs[i].real(x);
maxs[i].imag(y);
cin >> h[i];
// if(z == 24)printf(" %.0f \n",h[i]);
}
double ans = 1e9;
for (int i = 0; i < n; i++) {
double d = 1e9;
P p1 = mins[i], p3 = maxs[i];
P p2(mins[i].real(), maxs[i].imag());
P p4(maxs[i].real(), mins[i].imag());
if (p1.real() < s.real() && s.real() < p3.real() &&
p1.imag() < s.imag() && s.imag() < p3.imag()) {
ans = 0; /*
if(z==24){
printf("%.0f %.0f %.0f
%.0f\n",p1.real(),p1.imag(),p3.real(),p3.imag()); cout << "break" <<
endl;}*/
break;
}
d = min(d, distance(s, g, p1, p2));
d = min(d, distance(s, g, p2, p3));
d = min(d, distance(s, g, p3, p4));
d = min(d, distance(s, g, p4, p1));
double r = (h[i] * h[i] + d * d) / (2 * h[i]);
if (h[i] < d) {
ans = min(ans, r);
} else {
ans = min(ans, d);
}
}
printf("%.4f\n", ans);
}
return 0;
}
|
[["-", 51, 16, 12, 16, 12, 2, 63, 118, 119, 120], ["+", 51, 16, 12, 16, 12, 2, 63, 118, 119, 120], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
| 1
| 1,081
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
// 外積 (cross product) : a×b = |a||b|sinΘ
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// 内積 (dot product) : a・b = |a||b|cosΘ
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// 点a,bを通る直線と点cとの距離
double distance_l_p(P a, P b, P c) {
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(P a, P b, P c) {
if (dot(b - a, c - a) < EPS)
return abs(c - a);
if (dot(a - b, c - b) < EPS)
return abs(c - b);
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点cが線分a,b上にあるかないか(2)
int is_point_on_line(P a, P b, P c) {
// |a-c| + |c-b| <= |a-b| なら線分上
return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_ls(P a1, P a2, P b1, P b2) {
if ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS) &&
(cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < -EPS))
return true;
return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2) ||
is_point_on_line(b1, b2, a1) || is_point_on_line(b1, b2, a2);
}
struct block {
int minx, miny, maxx, maxy, h;
P p(int n) {
switch (n) {
case 0:
return P(minx, miny);
case 1:
return P(minx, maxy);
case 2:
return P(maxx, maxy);
case 3:
return P(maxx, miny);
}
}
};
int main() {
cout.precision(16);
int n;
while (cin >> n, n) {
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
vector<block> blocks(n);
REP(i, n) {
cin >> blocks[i].minx >> blocks[i].miny >> blocks[i].maxx >>
blocks[i].maxy >> blocks[i].h;
}
double ans = INT_MAX;
bool itsc = false;
REP(i, n) {
REP(j, 4) {
if (is_intersected_ls(P(sx, sy), P(ex, ey), blocks[i].p(j),
blocks[i].p((j + 1) % 4))) {
itsc = true;
break;
}
if (sx + EPS > blocks[i].minx && sx - EPS < blocks[i].maxx &&
sy + EPS > blocks[i].miny && sy - EPS < blocks[i].maxy) {
itsc = true;
break;
}
if (ex + EPS > blocks[i].minx && ex - EPS < blocks[i].maxx &&
ey + EPS > blocks[i].miny && ey - EPS < blocks[i].maxy) {
itsc = true;
break;
}
}
if (itsc) {
break;
}
}
if (itsc) {
cout << 0 << endl;
} else {
REP(i, n) {
double r = INT_MAX;
REP(j, 4) {
double d = distance_l_p(P(sx, sy), P(ex, ey), blocks[i].p(j));
r = min(r, (blocks[i].h * blocks[i].h + d * d) / 2 / blocks[i].h);
double ds = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(sx, sy));
r = min(r, (blocks[i].h * blocks[i].h + ds * ds) / 2 / blocks[i].h);
double de = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(ex, ey));
r = min(r, (blocks[i].h * blocks[i].h + de * de) / 2 / blocks[i].h);
}
if (blocks[i].h >= r) {
REP(j, 4) {
double d = distance_l_p(P(sx, sy), P(ex, ey), blocks[i].p(j));
r = min(r, d);
double ds = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(sx, sy));
r = min(r, ds);
double de = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(ex, ey));
r = min(r, de);
}
}
ans = min(ans, r);
}
cout << ans << endl;
}
}
}
|
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
// 外積 (cross product) : a×b = |a||b|sinΘ
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
// 内積 (dot product) : a・b = |a||b|cosΘ
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
// 点a,bを通る直線と点cとの距離
double distance_l_p(P a, P b, P c) {
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(P a, P b, P c) {
if (dot(b - a, c - a) < EPS)
return abs(c - a);
if (dot(a - b, c - b) < EPS)
return abs(c - b);
return abs(cross(b - a, c - a)) / abs(b - a);
}
// 点cが線分a,b上にあるかないか(2)
int is_point_on_line(P a, P b, P c) {
// |a-c| + |c-b| <= |a-b| なら線分上
return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}
// a1,a2を端点とする線分とb1,b2を端点とする線分の交差判定
int is_intersected_ls(P a1, P a2, P b1, P b2) {
if ((cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS) &&
(cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < -EPS))
return true;
return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2) ||
is_point_on_line(b1, b2, a1) || is_point_on_line(b1, b2, a2);
}
struct block {
int minx, miny, maxx, maxy, h;
P p(int n) {
switch (n) {
case 0:
return P(minx, miny);
case 1:
return P(minx, maxy);
case 2:
return P(maxx, maxy);
case 3:
return P(maxx, miny);
}
}
};
int main() {
cout.precision(16);
int n;
while (cin >> n, n) {
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
vector<block> blocks(n);
REP(i, n) {
cin >> blocks[i].minx >> blocks[i].miny >> blocks[i].maxx >>
blocks[i].maxy >> blocks[i].h;
}
double ans = INT_MAX;
bool itsc = false;
REP(i, n) {
REP(j, 4) {
if (is_intersected_ls(P(sx, sy), P(ex, ey), blocks[i].p(j),
blocks[i].p((j + 1) % 4))) {
itsc = true;
break;
}
if (sx + EPS > blocks[i].minx && sx - EPS < blocks[i].maxx &&
sy + EPS > blocks[i].miny && sy - EPS < blocks[i].maxy) {
itsc = true;
break;
}
if (ex + EPS > blocks[i].minx && ex - EPS < blocks[i].maxx &&
ey + EPS > blocks[i].miny && ey - EPS < blocks[i].maxy) {
itsc = true;
break;
}
}
if (itsc) {
break;
}
}
if (itsc) {
cout << 0 << endl;
} else {
REP(i, n) {
double r = INT_MAX;
REP(j, 4) {
double d = distance_ls_p(P(sx, sy), P(ex, ey), blocks[i].p(j));
r = min(r, (blocks[i].h * blocks[i].h + d * d) / 2 / blocks[i].h);
double ds = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(sx, sy));
r = min(r, (blocks[i].h * blocks[i].h + ds * ds) / 2 / blocks[i].h);
double de = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(ex, ey));
r = min(r, (blocks[i].h * blocks[i].h + de * de) / 2 / blocks[i].h);
}
if (blocks[i].h >= r) {
REP(j, 4) {
double d = distance_ls_p(P(sx, sy), P(ex, ey), blocks[i].p(j));
r = min(r, d);
double ds = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(sx, sy));
r = min(r, ds);
double de = distance_ls_p(blocks[i].p(j), blocks[i].p((j + 1) % 4),
P(ex, ey));
r = min(r, de);
}
}
ans = min(ans, r);
}
cout << ans << endl;
}
}
}
|
[["-", 0, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 0, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
| 1
| 1,352
|
#include <stdio.h>
int cnt_koisi[50];
int cheak_isi(int nin, int hito) {
int h;
for (h = 0; h < nin; h++) {
if (h != hito) {
if (cnt_koisi[h] > 0)
return 0;
}
}
return 1;
}
int main(void) {
int n, p, i;
int turn = 0;
while (1) {
scanf("%d %d", &n, &p);
if (n == 0 && p == 0)
break;
turn = 0;
for (i = 0; i < n; i++) {
cnt_koisi[i] = 0;
}
while (1) {
if (p > 0) { //回ってきたとき
p--;
cnt_koisi[turn]++;
if (p == 0) { //一つ取った時
if (cheak_isi(n, turn)) {
printf("%d", turn);
break;
}
}
} else { //お椀の中に入ってなかった時
p = cnt_koisi[turn];
cnt_koisi[turn] = 0;
}
turn++;
if (turn > n - 1)
turn = 0;
}
}
return 0;
}
|
#include <stdio.h>
int cnt_koisi[50];
int cheak_isi(int nin, int hito) {
int h;
for (h = 0; h < nin; h++) {
if (h != hito) {
if (cnt_koisi[h] > 0)
return 0;
}
}
return 1;
}
int main(void) {
int n, p, i;
int turn = 0;
while (1) {
scanf("%d %d", &n, &p);
if (n == 0 && p == 0)
break;
turn = 0;
for (i = 0; i < n; i++) {
cnt_koisi[i] = 0;
}
while (1) {
if (p > 0) { //回ってきたとき
p--;
cnt_koisi[turn]++;
if (p == 0) { //一つ取った時
if (cheak_isi(n, turn)) {
printf("%d\n", turn);
break;
}
}
} else { //お椀の中に入ってなかった時
p = cnt_koisi[turn];
cnt_koisi[turn] = 0;
}
turn++;
if (turn > n - 1)
turn = 0;
}
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 0
| 227
|
answers = []
def get_answer(n, p, answers)
owan = p
index = 0
temoto = []
n.times do
temoto << 0
end
while true
if owan == 0
owan += temoto[index]
temoto[index] = 0
else
if owan == 1 && temoto[index] == p -1
answers << index
return answers
end
owan -= 1
temoto[index] += 1
end
index += 1
index = 0 if index == n
end
end
while true
array = gets.chomp.split(" ").map(&:to_i)
n = array[0].to_i
p = array[1].to_i
exit if n == 0 && p == 0
answers = get_answer(n,p, answers)
end
answers.each do |ans|
puts ans
end
|
answers = []
def get_answer(n, p, answers)
owan = p
index = 0
temoto = []
n.times do
temoto << 0
end
while true
if owan == 0
owan += temoto[index]
temoto[index] = 0
else
if owan == 1 && temoto[index] == p -1
answers << index
return answers
end
owan -= 1
temoto[index] += 1
end
index += 1
index = 0 if index == n
end
end
while true
array = gets.chomp.split(" ").map(&:to_i)
n = array[0].to_i
p = array[1].to_i
break if n == 0 && p == 0
answers = get_answer(n,p, answers)
end
answers.each do |ans|
puts ans
end
|
[["-", 0, 493, 0, 89, 8, 170, 0, 751, 8, 22], ["+", 0, 89, 8, 170, 0, 751, 8, 94, 0, 94]]
| 4
| 157
|
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
while (true) {
int w = nextInt();
if (w == 0) {
break;
}
int h = nextInt();
int[][] map = new int[h][w];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = nextInt();
}
}
int island = 0;
Deque<Integer[]> deque = new ArrayDeque<Integer[]>();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[i][j] == 1) {
island++;
Integer[] land = {i, j};
deque.addFirst(land);
map[i][j] = 0;
while (deque.size() > 0) {
Integer[] pop = deque.pollFirst();
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (pop[0] + k >= 0 && pop[0] + k < map.length &&
pop[1] + l >= 0 && pop[1] + l < map[0].length &&
map[pop[0] + k][pop[1] + l] == 1) {
Integer[] push = {pop[0] + k, pop[1] + l};
deque.addFirst(push);
map[pop[0] + k][pop[1] + l] = 0;
}
}
}
}
}
}
}
System.out.println(island);
}
}
static int nextInt() {
int c;
try {
c = System.in.read();
while (c != '-' && (c < '0' || c > '9'))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
while (c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = System.in.read();
}
return res;
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
}
|
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
while (true) {
int w = nextInt();
if (w == 0) {
break;
}
int h = nextInt();
int[][] map = new int[h][w];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = nextInt();
}
}
int island = 0;
Deque<Integer[]> deque = new ArrayDeque<Integer[]>();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == 1) {
island++;
Integer[] land = {i, j};
deque.addFirst(land);
map[i][j] = 0;
while (deque.size() > 0) {
Integer[] pop = deque.pollFirst();
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (pop[0] + k >= 0 && pop[0] + k < map.length &&
pop[1] + l >= 0 && pop[1] + l < map[0].length &&
map[pop[0] + k][pop[1] + l] == 1) {
Integer[] push = {pop[0] + k, pop[1] + l};
deque.addFirst(push);
map[pop[0] + k][pop[1] + l] = 0;
}
}
}
}
}
}
}
System.out.println(island);
}
}
static int nextInt() {
int c;
try {
c = System.in.read();
while (c != '-' && (c < '0' || c > '9'))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
while (c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = System.in.read();
}
return res;
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
}
|
[["+", 0, 7, 15, 16, 12, 509, 500, 504, 0, 70], ["+", 0, 7, 15, 16, 12, 509, 500, 504, 71, 22], ["+", 0, 7, 15, 16, 12, 509, 500, 504, 0, 73]]
| 3
| 538
|
import java.util.ArrayDeque;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] aaa) {
Scanner s = new Scanner(System.in);
Stack<Integer> x = new Stack<Integer>();
Stack<Integer> y = new Stack<Integer>();
int h, w, i, j, k, l, count = 0, xx, yy, nx, ny;
int[][] memo = new int[51][51];
while (true) {
h = s.nextInt();
w = s.nextInt();
if (h == 0 && w == 0)
return;
for (i = 1; i <= w; i++) {
for (j = 1; j <= h; j++) {
memo[i][j] = s.nextInt();
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (memo[i][j] == 1) {
// System.out.println("b"+(i)+" "+(j));
memo[i][j] = 0;
x.push(i);
y.push(j);
// System.out.println("now"+now.x+" "+now.y);
while (x.size() > 0 && y.size() > 0) {
nx = x.pop();
ny = y.pop();
// System.out.println("a"+now.x+" "+now.y);
for (k = -1; k <= 1; k++) {
for (l = -1; l <= 1; l++) {
// if(nx==1)System.out.println("c"+(nx+k)+" "+(ny+l));
if ((nx + k) >= 1 && (nx + k) <= h && (ny + l) >= 1 &&
(ny + l) <= w && memo[nx + k][ny + l] == 1) {
memo[nx + k][ny + l] = 0;
x.push(nx + k);
y.push(ny + l);
// System.out.println("a"+(nx+k)+" "+(ny+l));
// System.out.println(x.size()+" "+y.size());
}
}
}
}
count++;
}
}
}
System.out.println(count);
count = 0;
for (i = 1; i <= 50; i++)
for (j = 1; j <= 50; j++)
memo[i][j] = 0;
}
}
}
|
import java.util.ArrayDeque;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] aaa) {
Scanner s = new Scanner(System.in);
Stack<Integer> x = new Stack<Integer>();
Stack<Integer> y = new Stack<Integer>();
int h, w, i, j, k, l, count = 0, xx, yy, nx, ny;
int[][] memo = new int[51][51];
while (true) {
h = s.nextInt();
w = s.nextInt();
if (h == 0 && w == 0)
break;
for (i = 1; i <= w; i++) {
for (j = 1; j <= h; j++) {
memo[i][j] = s.nextInt();
}
}
for (i = 1; i <= w; i++) {
for (j = 1; j <= h; j++) {
if (memo[i][j] == 1) {
// System.out.println("b"+(i)+" "+(j));
memo[i][j] = 0;
x.push(i);
y.push(j);
// System.out.println("now"+now.x+" "+now.y);
while (x.size() > 0 && y.size() > 0) {
nx = x.pop();
ny = y.pop();
// System.out.println("a"+now.x+" "+now.y);
for (k = -1; k <= 1; k++) {
for (l = -1; l <= 1; l++) {
// if(nx==1)System.out.println("c"+(nx+k)+" "+(ny+l));
if ((nx + k) >= 1 && (nx + k) <= w && (ny + l) >= 1 &&
(ny + l) <= h && memo[nx + k][ny + l] == 1) {
memo[nx + k][ny + l] = 0;
x.push(nx + k);
y.push(ny + l);
// System.out.println("a"+(nx+k)+" "+(ny+l));
// System.out.println(x.size()+" "+y.size());
}
}
}
}
count++;
}
}
}
System.out.println(count);
count = 0;
for (i = 1; i <= 50; i++)
for (j = 1; j <= 50; j++)
memo[i][j] = 0;
}
}
}
|
[["-", 0, 52, 8, 196, 0, 57, 64, 37, 0, 38], ["+", 0, 52, 8, 196, 0, 57, 64, 93, 0, 94], ["-", 0, 52, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 196, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 196, 0, 7, 15, 16, 12, 22], ["-", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["+", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["-", 15, 15, 0, 16, 31, 16, 12, 16, 12, 22], ["+", 15, 15, 0, 16, 31, 16, 12, 16, 12, 22]]
| 3
| 472
|
import java.util.*;
public class Main {
static int w;
static int h;
static int[][] fi;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
w = sc.nextInt();
h = sc.nextInt();
if (w == 0 && h == 0)
break;
fi = new int[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
fi[i][j] = sc.nextInt();
int n = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (fi[i][j] == 1)
n += loop(j, i);
System.out.println(n);
}
sc.close();
}
public static int loop(int x, int y) {
fi[x][y] = 0;
int[] dx = {0, 1, 1, 1, 0, -1, -1, -1};
int[] dy = {1, 1, 0, -1, -1, -1, 0, 1};
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx <= w && ny >= 0 && ny <= h) {
if (fi[nx][ny] == 1)
loop(nx, ny);
}
}
return 1;
}
}
|
import java.util.*;
public class Main {
static int w;
static int h;
static int[][] fi;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
w = sc.nextInt();
h = sc.nextInt();
if (w == 0 && h == 0)
break;
fi = new int[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
fi[i][j] = sc.nextInt();
int n = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (fi[i][j] == 1)
n += loop(j, i);
System.out.println(n);
}
sc.close();
}
public static int loop(int x, int y) {
fi[y][x] = 0;
int[] dx = {0, 1, 1, 1, 0, -1, -1, -1};
int[] dy = {1, 1, 0, -1, -1, -1, 0, 1};
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
if (fi[ny][nx] == 1)
loop(nx, ny);
}
}
return 1;
}
}
|
[["-", 0, 1, 0, 11, 31, 504, 516, 504, 71, 22], ["+", 0, 1, 0, 11, 31, 504, 516, 504, 71, 22], ["-", 8, 196, 0, 1, 0, 11, 31, 504, 71, 22], ["+", 8, 196, 0, 1, 0, 11, 31, 504, 71, 22], ["-", 0, 16, 31, 16, 31, 16, 12, 16, 17, 19], ["+", 0, 16, 31, 16, 31, 16, 12, 16, 17, 18], ["-", 0, 57, 15, 15, 0, 16, 12, 16, 17, 19], ["+", 0, 57, 15, 15, 0, 16, 12, 16, 17, 18], ["-", 15, 15, 0, 16, 31, 504, 516, 504, 71, 22], ["+", 15, 15, 0, 16, 31, 504, 516, 504, 71, 22], ["-", 0, 57, 15, 15, 0, 16, 31, 504, 71, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 504, 71, 22]]
| 3
| 360
|
#include <stdio.h>
int h, w;
int G[50][50];
void visit(int i, int j) {
int di[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dj[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int r;
int ni, nj;
G[i][j] = 0;
for (r = 0; r < 8; r++) {
ni = i + di[r];
nj = j + dj[r];
if (-1 < ni && ni < w && -1 < nj && nj < h && G[ni][nj] == 1)
visit(ni, nj);
}
}
main(void) {
int count = 0;
int i, j;
while (1) {
scanf("%d %d", &w, &h);
if (h == 0 && w == 0)
break;
for (i = 0; i < w; i++) {
for (j = 0; j < h; j++) {
scanf("%d", &G[i][j]);
}
}
for (i = 0; i < w; i++) {
for (j = 0; j < h; j++) {
if (G[i][j] == 1) {
count++;
visit(i, j);
}
}
}
printf("%d\n", count);
count = 0;
}
return 0;
}
|
#include <stdio.h>
int h, w;
int G[50][50];
void visit(int i, int j) {
int di[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dj[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int r;
int ni, nj;
G[i][j] = 0;
for (r = 0; r < 8; r++) {
ni = i + di[r];
nj = j + dj[r];
if (-1 < ni && ni < h && -1 < nj && nj < w && G[ni][nj] == 1)
visit(ni, nj);
}
}
main(void) {
int count = 0;
int i, j;
while (1) {
scanf("%d %d", &w, &h);
if (h == 0 && w == 0)
break;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
scanf("%d", &G[i][j]);
}
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (G[i][j] == 1) {
count++;
visit(i, j);
}
}
}
printf("%d\n", count);
count = 0;
}
return 0;
}
|
[["-", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["+", 31, 16, 31, 16, 31, 16, 12, 16, 12, 22], ["-", 15, 23, 0, 16, 31, 16, 12, 16, 12, 22], ["+", 15, 23, 0, 16, 31, 16, 12, 16, 12, 22], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
| 0
| 325
|
#include <stdio.h>
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int map[52][52];
void foo(int y, int x, int n) {
int i;
if (map[y][x] == -1) {
map[y][x] = n;
for (i = 0; i < 8; i++) {
foo(x + dx[i], y + dy[i], n);
}
}
}
int main() {
int w, h, i, j, ans;
while (1) {
scanf("%d %d", &w, &h);
if (w == 0 && h == 0) {
break;
}
ans = 0;
for (i = 0; i < h; i++) {
map[i][0] = 0;
map[i][h + 1] = 0;
}
for (j = 0; j < w; j++) {
map[0][j] = 0;
map[w + 1][j] = 0;
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
scanf("%d", &map[i][j]);
map[i][j] *= -1;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (map[i][j] == -1) {
ans++;
foo(i, j, ans);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
#include <stdio.h>
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int map[52][52];
void foo(int y, int x, int n) {
int i;
if (map[y][x] == -1) {
map[y][x] = n;
for (i = 0; i < 8; i++) {
foo(y + dy[i], x + dx[i], n);
}
}
}
int main() {
int w, h, i, j, ans;
while (1) {
scanf("%d %d", &w, &h);
if (w == 0 && h == 0) {
break;
}
ans = 0;
for (i = 0; i < h; i++) {
map[i][0] = 0;
map[i][h + 1] = 0;
}
for (j = 0; j < w; j++) {
map[0][j] = 0;
map[w + 1][j] = 0;
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
scanf("%d", &map[i][j]);
map[i][j] *= -1;
}
}
for (i = 1; i <= h; i++) {
for (j = 1; j <= w; j++) {
if (map[i][j] == -1) {
ans++;
foo(i, j, ans);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 31, 22], ["-", 0, 2, 3, 4, 0, 16, 12, 69, 28, 22], ["+", 0, 2, 3, 4, 0, 16, 12, 69, 28, 22]]
| 0
| 387
|
import queue
def proceess(start_x,start_y):
#insland = []
#insland.append([start_x,start_y])
new_island = queue.Queue()
map[start_x][start_y] = 0
new_island.put([start_x,start_y])
while True:
ob = new_island.get()
x = ob[0]
y = ob[1]
#print("x : " + str(x) + ", y : " + str(y))
for i in [-1,0,1]:
for j in [-1,0,1]:
nx = x + i
ny = y + j
if nx >= 0 and ny >= 0 and nx < w and ny < h:
#print(map[nx][ny])
if map[nx][ny] == 1:
#insland.append([nx,ny])
#print("nx : " + str(nx) + ", ny : " + str(ny))
new_island.put([nx,ny])
map[nx][ny] = 0
if new_island.empty():
break
while True:
map = []
tmp = input().split(" ")
w = int(tmp[0])
h = int(tmp[1])
if w == 1 and h == 1:
break
for j in range(h):
nova = []
arm = input()
for k in arm.split(" "):
#k.rstrip('\n')
nova.append(int(k))
map.append(nova)
#print(map)
count = 0
for i in range(w):
for j in range(h):
if map[i][j] == 1:
#print("i : " + str(i) + ", j : " + str(j))
proceess(i,j)
count += 1
print(count)
|
import queue
def proceess(start_x,start_y):
#print("start_x : " + str(start_x) + ", start_y : " + str(start_y))
#insland = []
#insland.append([start_x,start_y])
new_island = queue.Queue()
map[start_x][start_y] = 0
new_island.put([start_x,start_y])
while True:
ob = new_island.get()
x = ob[0]
y = ob[1]
#print("x : " + str(x) + ", y : " + str(y))
for i in [-1,0,1]:
for j in [-1,0,1]:
nx = x + i
ny = y + j
if nx >= 0 and ny >= 0 and nx < h and ny < w:
if map[nx][ny] == 1:
#insland.append([nx,ny])
#print("ny : " + str(ny) + ", nx : " + str(nx))
new_island.put([nx,ny])
map[nx][ny] = 0
#print("end 8 count.")
if new_island.empty():
break
while True:
map = []
tmp = input().split(" ")
w = int(tmp[0])
h = int(tmp[1])
if w == 0 and h == 0:
break
for j in range(h):
nova = []
arm = input()
for k in arm.split(" "):
#k.rstrip('\n')
nova.append(int(k))
map.append(nova)
#print(map)
count = 0
for i in range(h):
for j in range(w):
if map[i][j] == 1:
#print("i : " + str(i) + ", j : " + str(j))
proceess(i,j)
count += 1
print(count)
|
[["-", 0, 57, 15, 679, 31, 679, 12, 666, 0, 22], ["+", 0, 57, 15, 679, 31, 679, 12, 666, 0, 22], ["-", 8, 196, 0, 57, 15, 679, 12, 666, 0, 22], ["+", 8, 196, 0, 57, 15, 679, 12, 666, 0, 22], ["-", 8, 196, 0, 57, 15, 679, 31, 666, 0, 612], ["+", 8, 196, 0, 57, 15, 679, 31, 666, 0, 612], ["-", 8, 196, 0, 57, 15, 679, 12, 666, 0, 612], ["+", 8, 196, 0, 57, 15, 679, 12, 666, 0, 612], ["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 22], ["+", 8, 196, 0, 7, 12, 652, 3, 4, 0, 22]]
| 5
| 290
|
import sys
def dfs(tiles, W, H, x, y):
tiles[y][x] = '0'
for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
dfs(tiles, W, H, x+dx, y+dy)
return
def main():
while True:
W,H = [int(x) for x in input().split()]
if W==0 and H==0:
exit()
tiles = [[c for c in input().split()] for h in range(H)]
cnt = 0
for h in range(H):
for w in range(W):
if tiles[h][w] == '1':
cnt += 1
dfs(tiles, W, H, w, h)
print(cnt)
if __name__ == '__main__':
sys.getrecursionlimit(10000)
main()
|
import sys
def dfs(tiles, W, H, x, y):
tiles[y][x] = '0'
for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
dfs(tiles, W, H, x+dx, y+dy)
return
def main():
while True:
W,H = [int(x) for x in input().split()]
if W==0 and H==0:
exit()
tiles = [[c for c in input().split()] for h in range(H)]
cnt = 0
for h in range(H):
for w in range(W):
if tiles[h][w] == '1':
cnt += 1
dfs(tiles, W, H, w, h)
print(cnt)
if __name__ == '__main__':
sys.setrecursionlimit(100000)
main()
|
[["-", 64, 196, 0, 1, 0, 652, 63, 319, 319, 22], ["+", 64, 196, 0, 1, 0, 652, 63, 319, 319, 22], ["-", 64, 196, 0, 1, 0, 652, 3, 4, 0, 612], ["+", 64, 196, 0, 1, 0, 652, 3, 4, 0, 612]]
| 5
| 269
|
import sys
sys.setrecursionlimit(1<<32)
def f(x,y):
a[y][x]='0'
for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
dx+=x;dy+=y
if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
w,h=map(int,input().split())
if w==0:break
a=[list(input().split()) for _ in[0]*h];b=0
for i in range(w):
for j in range(h):
if a[j][i]=='1':
b+=1;f(i,j)
print(b)
|
import sys
sys.setrecursionlimit(10000)
def f(x,y):
a[y][x]='0'
for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:
dx+=x;dy+=y
if 0<=dx<w and 0<=dy<h and a[dy][dx]=='1':f(dx,dy)
while 1:
w,h=map(int,input().split())
if w==0:break
a=[list(input().split()) for _ in[0]*h];b=0
for i in range(w):
for j in range(h):
if a[j][i]=='1':
b+=1;f(i,j)
print(b)
|
[["-", 0, 1, 0, 652, 3, 4, 0, 657, 31, 612], ["-", 0, 1, 0, 652, 3, 4, 0, 657, 17, 151], ["-", 0, 1, 0, 652, 3, 4, 0, 657, 12, 612], ["+", 0, 656, 0, 1, 0, 652, 3, 4, 0, 612]]
| 5
| 222
|
if __name__ == '__main__':
while 1:
w,h = list(map(int,input().strip().split()))
if w == h ==0:break
imap = [list(map(int,input().strip().split())) for _ in range(h)]
visited = []
count = 0
for j in range(h):
for i in range(w):
stack =[]
if not (j,i) in visited and imap[j][i] == 1:
count +=1
stack.append((j,i))
visited.append(((j,i)))
while len(stack) !=0:
y,x = stack.pop()
if y>0 and imap[y-1][x] == 1 and not (y-1,x) in visited:
visited.append((y-1,x))
stack.append((y-1,x))
if x>0 and imap[y][x-1] == 1 and not (y,x-1) in visited:
visited.append(((y,x-1)))
stack.append((y,x))
if y>0 and x>0 and imap[y-1][x-1] == 1 and not (y-1,x-1) in visited:
visited.append((y-1,x-1))
stack.append((y-1,x-1))
if y>0 and x<w-1 and imap[y-1][x+1] ==1 and not (y-1, x+1) in visited:
visited.append((y-1, x+1))
stack.append((y-1, x+1))
if y <h-1 and x>0 and imap[y+1][x-1] ==1 and not (y+1,x-1) in visited:
visited.append((y+1,x-1))
stack.append((y+1,x-1))
if y < h-1 and x < w-1 and imap[y+1][x+1] ==1and not (y+1,x+1) in visited:
visited.append((y+1,x+1))
stack.append((y+1,x+1))
if y < h-1 and imap[y+1][x]== 1 and not (y+1,x) in visited:
visited.append((y+1,x))
stack.append((y+1,x))
if x < w-1 and imap[y][x+1] == 1 and not (y,x+1) in visited:
visited.append((y,x+1))
stack.append((y,x+1))
print(count)
|
if __name__ == '__main__':
while 1:
w,h = list(map(int,input().strip().split()))
if w == h ==0:break
imap = [list(map(int,input().strip().split())) for _ in range(h)]
visited = []
count = 0
for j in range(h):
for i in range(w):
stack =[]
if not (j,i) in visited and imap[j][i] == 1:
count +=1
stack.append((j,i))
visited.append(((j,i)))
while len(stack) !=0:
y,x = stack.pop()
if y>0 and imap[y-1][x] == 1 and not (y-1,x) in visited:
visited.append((y-1,x))
stack.append((y-1,x))
if x>0 and imap[y][x-1] == 1 and not (y,x-1) in visited:
visited.append(((y,x-1)))
stack.append((y,x-1))
if y>0 and x>0 and imap[y-1][x-1] == 1 and not (y-1,x-1) in visited:
visited.append((y-1,x-1))
stack.append((y-1,x-1))
if y>0 and x<w-1 and imap[y-1][x+1] ==1 and not (y-1, x+1) in visited:
visited.append((y-1, x+1))
stack.append((y-1, x+1))
if y <h-1 and x>0 and imap[y+1][x-1] ==1 and not (y+1,x-1) in visited:
visited.append((y+1,x-1))
stack.append((y+1,x-1))
if y < h-1 and x < w-1 and imap[y+1][x+1] ==1and not (y+1,x+1) in visited:
visited.append((y+1,x+1))
stack.append((y+1,x+1))
if y < h-1 and imap[y+1][x]== 1 and not (y+1,x) in visited:
visited.append((y+1,x))
stack.append((y+1,x))
if x < w-1 and imap[y][x+1] == 1 and not (y,x+1) in visited:
visited.append((y,x+1))
stack.append((y,x+1))
print(count)
|
[["+", 0, 652, 3, 4, 0, 660, 0, 657, 17, 33], ["+", 0, 652, 3, 4, 0, 660, 0, 657, 12, 612]]
| 5
| 640
|
import sys
sys.setrecursionlimit(10000)
def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
l[y-1][x+1]=0
fill(y-1,x+1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[map(int,input().split()) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
import sys
sys.setrecursionlimit(10000)
def fill(y,x):
global w,h,l
if 0<=y<h and 0<=x+1<w and l[y][x+1]==1:
l[y][x+1]=0
fill(y,x+1)
if 0<=y<h and 0<=x-1<w and l[y][x-1]==1:
l[y][x-1]=0
fill(y,x-1)
if 0<=y+1<h and 0<=x<w and l[y+1][x]==1:
l[y+1][x]=0
fill(y+1,x)
if 0<=y-1<h and 0<=x<w and l[y-1][x]==1:
l[y-1][x]=0
fill(y-1,x)
if 0<=y+1<h and 0<=x+1<w and l[y+1][x+1]==1:
l[y+1][x+1]=0
fill(y+1,x+1)
if 0<=y-1<h and 0<=x+1<w and l[y-1][x+1]==1:
l[y-1][x+1]=0
fill(y-1,x+1)
if 0<=y+1<h and 0<=x-1<w and l[y+1][x-1]==1:
l[y+1][x-1]=0
fill(y+1,x-1)
if 0<=y-1<h and 0<=x-1<w and l[y-1][x-1]==1:
l[y-1][x-1]=0
fill(y-1,x-1)
while 1:
w,h=map(int,input().split())
if not w and not h:break
l=[list(map(int,input().split())) for _ in range(h)]
cnt=0
while 1:
cds=[[i,j.index(1)] for (i,j) in enumerate(l) if 1 in j]
if not cds:
break
else:
y,x=cds[0]
l[y][x]=0
fill(y,x)
cnt+=1
print(cnt)
|
[["+", 0, 1, 0, 662, 12, 658, 8, 652, 63, 22], ["+", 0, 662, 12, 658, 8, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
| 5
| 544
|
#include <iostream>
using namespace std;
/*bool check(const int x, const int y, const int LandNum, const int
aNodeCol[50][50], const int aXMax, const int aYMax){ if ((x<0 || x >= aXMax) ||
(y<0 || y >= aYMax) || (aNodeCol[x][y] != 1)){ return false;
}
return true;
}*/
bool LandConfirmedAndMawariCheck(const int x, const int y, const int LandNum,
int aNodeCol[50][50], const int aXMax,
const int aYMax) {
if (aNodeCol[x][y] == 1) {
aNodeCol[x][y] = LandNum;
/*if (check(x - 1, y, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax,
aYMax); if (check(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax,
aYMax);*/
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y < 0 || y >= aYMax) ||
(aNodeCol[x - 1][y] != 1)))
LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax, aYMax);
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x - 1][y - 1] != 1)))
LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x - 1][y + 1] != 1)))
LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x + 1][y - 1] != 1)))
LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y < 0 || y >= aYMax) ||
(aNodeCol[x + 1][y] != 1)))
LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax, aYMax);
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x + 1][y + 1] != 1)))
LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x < 0 || x >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x][y - 1] != 1)))
LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax, aYMax);
if (!((x < 0 || x >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x][y + 1] != 1)))
LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax, aYMax);
return true;
}
return false;
}
int main(void) {
int NodeCol[50][50];
int a, b, c;
int XMax, YMax;
while (1) {
cin >> a >> b;
if (a == 0 && b == 0)
break;
XMax = a;
YMax = b;
for (int Lone = 0; Lone < XMax; ++Lone) {
for (int Ltwo = 0; Ltwo < YMax; ++Ltwo) {
cin >> c;
NodeCol[Lone][Ltwo] = c;
}
}
int t = 2;
for (int Lone = 0; Lone < XMax; ++Lone) {
for (int Ltwo = 0; Ltwo < YMax; ++Ltwo) {
if (NodeCol[Lone][Ltwo] == 1) {
if (LandConfirmedAndMawariCheck(Lone, Ltwo, t, NodeCol, XMax, YMax))
t++;
}
}
}
cout << t - 2 << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
/*bool check(const int x, const int y, const int LandNum, const int
aNodeCol[50][50], const int aXMax, const int aYMax){ if ((x<0 || x >= aXMax) ||
(y<0 || y >= aYMax) || (aNodeCol[x][y] != 1)){ return false;
}
return true;
}*/
bool LandConfirmedAndMawariCheck(const int x, const int y, const int LandNum,
int aNodeCol[50][50], const int aXMax,
const int aYMax) {
if (aNodeCol[x][y] == 1) {
aNodeCol[x][y] = LandNum;
/*if (check(x - 1, y, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax,
aYMax); if (check(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax,
aYMax); if (check(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x, y - 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax,
aYMax); if (check(x, y + 1, LandNum, aNodeCol, aXMax,
aYMax))LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax,
aYMax);*/
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y < 0 || y >= aYMax) ||
(aNodeCol[x - 1][y] != 1)))
LandConfirmedAndMawariCheck(x - 1, y, LandNum, aNodeCol, aXMax, aYMax);
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x - 1][y - 1] != 1)))
LandConfirmedAndMawariCheck(x - 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x - 1 < 0 || x - 1 >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x - 1][y + 1] != 1))) {
LandConfirmedAndMawariCheck(x - 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax);
}
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x + 1][y - 1] != 1)))
LandConfirmedAndMawariCheck(x + 1, y - 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y < 0 || y >= aYMax) ||
(aNodeCol[x + 1][y] != 1)))
LandConfirmedAndMawariCheck(x + 1, y, LandNum, aNodeCol, aXMax, aYMax);
if (!((x + 1 < 0 || x + 1 >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x + 1][y + 1] != 1)))
LandConfirmedAndMawariCheck(x + 1, y + 1, LandNum, aNodeCol, aXMax,
aYMax);
if (!((x < 0 || x >= aXMax) || (y - 1 < 0 || y - 1 >= aYMax) ||
(aNodeCol[x][y - 1] != 1)))
LandConfirmedAndMawariCheck(x, y - 1, LandNum, aNodeCol, aXMax, aYMax);
if (!((x < 0 || x >= aXMax) || (y + 1 < 0 || y + 1 >= aYMax) ||
(aNodeCol[x][y + 1] != 1)))
LandConfirmedAndMawariCheck(x, y + 1, LandNum, aNodeCol, aXMax, aYMax);
return true;
}
return false;
}
int main(void) {
int NodeCol[50][50];
int a, b, c;
int XMax, YMax;
while (1) {
cin >> a >> b;
if (a == 0 && b == 0)
break;
XMax = a;
YMax = b;
for (int Lone = 0; Lone < YMax; ++Lone) {
for (int Ltwo = 0; Ltwo < XMax; ++Ltwo) {
cin >> c;
NodeCol[Ltwo][Lone] = c;
}
}
int t = 2;
for (int Lone = 0; Lone < XMax; ++Lone) {
for (int Ltwo = 0; Ltwo < YMax; ++Ltwo) {
if (NodeCol[Lone][Ltwo] == 1) {
if (LandConfirmedAndMawariCheck(Lone, Ltwo, t, NodeCol, XMax, YMax))
t++;
}
}
}
cout << t - 2 << endl;
}
return 0;
}
|
[["+", 0, 57, 64, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 57, 64, 9, 0, 57, 64, 9, 0, 46], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22], ["+", 0, 1, 0, 11, 31, 69, 341, 342, 0, 22]]
| 1
| 772
|
#include <algorithm>
#include <iostream>
using namespace std;
int c[50][50];
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ret;
int w, h;
void dfs(int x, int y) {
if (c[x][y] == 0)
return;
c[x][y] = 0;
for (int i = 0; i < 8; i++) {
if (x + dx[i] < 0 || x + dx[i] >= w)
continue;
if (y + dy[i] < 0 || y + dy[i] >= h)
continue;
dfs(x + dx[i], y + dy[i]);
}
}
int main() {
while (cin >> w >> h, w) {
ret = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[j][i];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == 1) {
ret++;
dfs(j, i);
}
}
}
cout << ret << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int c[50][50];
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int ret;
int w, h;
void dfs(int x, int y) {
if (c[x][y] == 0)
return;
c[x][y] = 0;
for (int i = 0; i < 8; i++) {
if (x + dx[i] < 0 || x + dx[i] >= w)
continue;
if (y + dy[i] < 0 || y + dy[i] >= h)
continue;
dfs(x + dx[i], y + dy[i]);
}
}
int main() {
while (cin >> w >> h, w) {
ret = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> c[j][i];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[j][i] == 1) {
ret++;
dfs(j, i);
}
}
}
cout << ret << endl;
}
return 0;
}
|
[["-", 51, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 51, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 22]]
| 1
| 314
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int x, y;
int o[52][52];
queue<int> q;
void tansaku(int j, int i) {
if (j < 1 || j > x || i < 1 || i > y || o[i][j] == 0 || o[i][j] == 2) {
return;
}
if (o[i][j] == 1) {
if (q.empty())
q.push(1);
o[i][j] = 2;
}
tansaku(j + 1, i);
tansaku(j - 1, i);
tansaku(j, i + 1);
tansaku(j, i - 1);
tansaku(j + 1, i + 1);
tansaku(j - 1, i - 1);
tansaku(j + 1, i - 1);
tansaku(j - 1, i + 1);
}
int main() {
while (cin >> x >> y, x != 0 || y != 0) {
int count = 0;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
cin >> o[i][j];
}
}
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
tansaku(j, i);
if (!q.empty()) {
count += q.front();
}
q.pop();
}
}
cout << count << endl;
}
}
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int x, y;
int o[52][52];
queue<int> q;
void tansaku(int j, int i) {
if (j < 1 || j > x || i < 1 || i > y || o[i][j] == 0 || o[i][j] == 2) {
return;
}
if (o[i][j] == 1) {
if (q.empty())
q.push(1);
o[i][j] = 2;
}
tansaku(j + 1, i);
tansaku(j - 1, i);
tansaku(j, i + 1);
tansaku(j, i - 1);
tansaku(j + 1, i + 1);
tansaku(j - 1, i - 1);
tansaku(j + 1, i - 1);
tansaku(j - 1, i + 1);
}
int main() {
while (cin >> x >> y, x != 0 || y != 0) {
int count = 0;
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
cin >> o[i][j];
}
}
for (int i = 1; i <= y; i++) {
for (int j = 1; j <= x; j++) {
tansaku(j, i);
if (!q.empty()) {
count += q.front();
q.pop();
}
}
}
cout << count << endl;
}
}
|
[["-", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
| 1
| 363
|
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int area[50][50], cnt = 0;
int w, h;
void dfs(int x, int y) {
area[x][y] = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx, ny = y + dy;
if (0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1) {
dfs(nx, ny);
}
}
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
rep(i, h) {
rep(j, w) { cin >> area[i][j]; }
}
rep(i, w) {
rep(j, h) {
if (area[i][j] == 1) {
dfs(i, j);
cnt++;
}
}
}
cout << cnt << '\n';
cnt = 0;
}
}
|
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int area[50][50], cnt = 0;
int w, h;
void dfs(int x, int y) {
area[x][y] = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int nx = x + dx, ny = y + dy;
if (0 <= nx && nx < w && 0 <= ny && ny < h && area[nx][ny] == 1) {
dfs(nx, ny);
}
}
}
}
int main() {
while (true) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
rep(j, h) {
rep(i, w) { cin >> area[i][j]; }
}
rep(i, w) {
rep(j, h) {
if (area[i][j] == 1) {
dfs(i, j);
cnt++;
}
}
}
cout << cnt << '\n';
cnt = 0;
}
}
|
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78]]
| 1
| 243
|
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
bool find1(vector<vector<int>> &C, vector<vector<int>> &check, int *findh,
int *findw) {
for (int i = 0; i < C.size(); ++i)
for (int j = 0; j < C[0].size(); ++j)
if (C[i][j] == 1 && check[i][j] == 0) {
*findh = i;
*findw = j;
return true;
}
return false;
}
bool find2(vector<vector<int>> &C, vector<vector<int>> &check, int *findh,
int *findw) {
if (C[*findh - 1][*findw - 1] == 1 && check[*findh - 1][*findw - 1] == 0) {
*findh -= 1;
*findw -= 1;
return true;
} else if (C[*findh - 1][*findw] == 1 && check[*findh - 1][*findw] == 0) {
*findh -= 1;
return true;
} else if (C[*findh - 1][*findw + 1] && check[*findh - 1][*findw + 1] == 0) {
*findh -= 1;
*findw += 1;
return true;
} else if (C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0) {
*findw -= 1;
return true;
} else if (C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0) {
*findw += 1;
return true;
} else if (C[*findh + 1][*findw - 1] == 1 &&
check[*findh + 1][*findw - 1] == 0) {
*findh += 1;
*findw -= 1;
return true;
} else if (C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0) {
*findh += 1;
return true;
} else if (C[*findh + 1][*findw + 1] == 1 &&
check[*findh + 1][*findw + 1] == 0) {
*findh += 1;
*findw += 1;
return true;
} else
return false;
}
int main() {
int w, h, findh, findw;
stack<pair<int, int>> finish;
queue<int> answer;
while (cin >> h >> w && !(w == 0 && h == 0)) {
int howmany = 0;
vector<vector<int>> C(h + 2, vector<int>(w + 2, 0)),
check(h + 2, vector<int>(w + 2, 0));
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
cin >> C[i][j];
while (find1(C, check, &findh, &findw)) {
check[findh][findw] = 1;
finish.push(make_pair(findh, findw));
while (!finish.empty()) {
findh = finish.top().first;
findw = finish.top().second;
while (find2(C, check, &findh, &findw)) {
finish.push(make_pair(findh, findw));
check[findh][findw] = 1;
}
finish.pop();
}
++howmany;
}
answer.push(howmany);
}
for (; !answer.empty(); answer.pop())
cout << answer.front() << endl;
}
|
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
bool find1(vector<vector<int>> &C, vector<vector<int>> &check, int *findh,
int *findw) {
for (int i = 0; i < C.size(); ++i)
for (int j = 0; j < C[0].size(); ++j)
if (C[i][j] == 1 && check[i][j] == 0) {
*findh = i;
*findw = j;
return true;
}
return false;
}
bool find2(vector<vector<int>> &C, vector<vector<int>> &check, int *findh,
int *findw) {
if (C[*findh - 1][*findw - 1] == 1 && check[*findh - 1][*findw - 1] == 0) {
*findh -= 1;
*findw -= 1;
return true;
} else if (C[*findh - 1][*findw] == 1 && check[*findh - 1][*findw] == 0) {
*findh -= 1;
return true;
} else if (C[*findh - 1][*findw + 1] == 1 &&
check[*findh - 1][*findw + 1] == 0) {
*findh -= 1;
*findw += 1;
return true;
} else if (C[*findh][*findw - 1] == 1 && check[*findh][*findw - 1] == 0) {
*findw -= 1;
return true;
} else if (C[*findh][*findw + 1] == 1 && check[*findh][*findw + 1] == 0) {
*findw += 1;
return true;
} else if (C[*findh + 1][*findw - 1] == 1 &&
check[*findh + 1][*findw - 1] == 0) {
*findh += 1;
*findw -= 1;
return true;
} else if (C[*findh + 1][*findw] == 1 && check[*findh + 1][*findw] == 0) {
*findh += 1;
return true;
} else if (C[*findh + 1][*findw + 1] == 1 &&
check[*findh + 1][*findw + 1] == 0) {
*findh += 1;
*findw += 1;
return true;
} else
return false;
}
int main() {
int w, h, findh, findw;
stack<pair<int, int>> finish;
queue<int> answer;
while (cin >> w >> h && !(w == 0 && h == 0)) {
int howmany = 0;
vector<vector<int>> C(h + 2, vector<int>(w + 2, 0)),
check(h + 2, vector<int>(w + 2, 0));
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
cin >> C[i][j];
while (find1(C, check, &findh, &findw)) {
check[findh][findw] = 1;
finish.push(make_pair(findh, findw));
while (!finish.empty()) {
findh = finish.top().first;
findw = finish.top().second;
while (find2(C, check, &findh, &findw)) {
finish.push(make_pair(findh, findw));
check[findh][findw] = 1;
}
finish.pop();
}
++howmany;
}
answer.push(howmany);
}
for (; !answer.empty(); answer.pop())
cout << answer.front() << endl;
}
|
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 60], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["-", 15, 339, 51, 16, 31, 16, 31, 16, 17, 152], ["-", 15, 339, 51, 16, 31, 16, 31, 16, 12, 22], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 17, 152], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 12, 22]]
| 1
| 816
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;
void sol(int x, int y) {
field[x][y] = 0;
for (int i = 0; i < 8; i++) {
if ((0 <= x + dx[i] && x + dx[i] < h) || (0 <= y + dy[i] && y + dy[i] < w))
if (field[x + dx[i]][y + dy[i]] == 1)
sol(x + dx[i], y + dy[i]);
}
}
int main() {
while (cin >> h >> w, h || w) {
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> field[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (field[i][j] == 1) {
res++;
sol(i, j);
}
}
}
cout << res << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1},
dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
int field[51][51];
int h, w;
void sol(int x, int y) {
field[x][y] = 0;
for (int i = 0; i < 8; i++) {
if ((0 <= x + dx[i] && x + dx[i] < h) || (0 <= y + dy[i] && y + dy[i] < w))
if (field[x + dx[i]][y + dy[i]] == 1)
sol(x + dx[i], y + dy[i]);
}
}
int main() {
while (cin >> w >> h, h || w) {
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> field[i][j];
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (field[i][j] == 1) {
res++;
sol(i, j);
}
}
}
cout << res << endl;
}
}
|
[["-", 15, 339, 51, 34, 31, 16, 31, 16, 12, 22], ["-", 0, 52, 15, 339, 51, 34, 31, 16, 17, 152], ["+", 0, 52, 15, 339, 51, 34, 31, 16, 17, 152], ["+", 0, 52, 15, 339, 51, 34, 31, 16, 12, 22]]
| 1
| 313
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
int read() {
int i;
scanf("%lld", &i);
return i;
}
int land[100][100] = {0};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int cnt = 0;
void isLand(int i, int j) {
if (land[i][j] == 1) {
land[i][j] = 0;
REP(k, 8) {
int newx = i + dy[k];
int newy = j + dx[k];
isLand(newy, newx);
}
}
}
signed main() {
int w, h;
while (w = read(), h = read(), w || h) {
cnt = 0;
REP(i, 100) {
REP(j, 100) { land[i][j] = 0; }
}
FOR(i, 1, h + 1) {
FOR(j, 1, w + 1) { land[i][j] = read(); }
}
FOR(i, 1, h + 1) {
FOR(j, 1, w + 1) {
if (land[i][j] == 1) {
cnt++;
isLand(i, j);
}
}
}
cout << cnt << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
int read() {
int i;
scanf("%lld", &i);
return i;
}
int land[100][100] = {0};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int cnt = 0;
void isLand(int i, int j) {
if (land[i][j] == 1) {
land[i][j] = 0;
REP(k, 8) {
int newy = i + dy[k];
int newx = j + dx[k];
isLand(newy, newx);
}
}
}
signed main() {
int w, h;
while (w = read(), h = read(), w || h) {
cnt = 0;
REP(i, 100) {
REP(j, 100) { land[i][j] = 0; }
}
FOR(i, 1, h + 1) {
FOR(j, 1, w + 1) { land[i][j] = read(); }
}
FOR(i, 1, h + 1) {
FOR(j, 1, w + 1) {
if (land[i][j] == 1) {
cnt++;
isLand(i, j);
}
}
}
cout << cnt << endl;
}
}
|
[["-", 64, 9, 0, 9, 0, 43, 49, 50, 49, 22], ["+", 64, 9, 0, 9, 0, 43, 49, 50, 49, 22]]
| 1
| 347
|
#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
bool dfs(int sx, int sy, vector<vector<bool>> &c) {
if (c[sx][sy] == false)
return false;
c[sx][sy] = false;
for (int i = 0; i < 8; i++) {
int nx = sx + dx[i];
int ny = sy + dy[i];
bool flag = dfs(nx, ny, c);
}
return true;
}
int main(void) {
int h, w, temp;
while (1) {
cin >> w >> h;
if (w == 0)
break;
vector<vector<bool>> c(w + 2, vector<bool>(h + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> temp;
if (temp == 1)
c[i][j] = true;
}
}
int t = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
bool flag = dfs(i, j, c);
if (flag)
t++;
}
}
cout << t << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
bool dfs(int sx, int sy, vector<vector<bool>> &c) {
if (c[sx][sy] == false)
return false;
c[sx][sy] = false;
for (int i = 0; i < 8; i++) {
int nx = sx + dx[i];
int ny = sy + dy[i];
bool flag = dfs(nx, ny, c);
}
return true;
}
int main(void) {
int h, w, temp;
while (1) {
cin >> w >> h;
if (w == 0)
break;
vector<vector<bool>> c(h + 2, vector<bool>(w + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> temp;
if (temp == 1)
c[i][j] = true;
}
}
int t = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
bool flag = dfs(i, j, c);
if (flag)
t++;
}
}
cout << t << endl;
}
return 0;
}
|
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["-", 51, 4, 0, 2, 3, 4, 0, 16, 31, 22], ["+", 51, 4, 0, 2, 3, 4, 0, 16, 31, 22]]
| 1
| 330
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 0, -1, 0, 1};
void dfs(VVI &table, int i, int j) {
table[i][j] = 0;
for (int loop = 0; loop < 8; loop++) {
int new_i = i + dy[loop];
int new_j = j + dx[loop];
if (table[new_i][new_j] == 1) {
dfs(table, new_i, new_j);
}
}
return;
}
int main(void) {
while (true) {
int w;
int h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
VVI table(h + 2, VI(w + 2, 0));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> table[i][j];
}
}
int number = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (table[i][j] == 1) {
dfs(table, i, j);
number++;
}
}
}
cout << number << endl;
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(VVI &table, int i, int j) {
table[i][j] = 0;
for (int loop = 0; loop < 8; loop++) {
int new_i = i + dy[loop];
int new_j = j + dx[loop];
if (table[new_i][new_j] == 1) {
dfs(table, new_i, new_j);
}
}
return;
}
int main(void) {
while (true) {
int w;
int h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
VVI table(h + 2, VI(w + 2, 0));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> table[i][j];
}
}
int number = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (table[i][j] == 1) {
dfs(table, i, j);
number++;
}
}
}
cout << number << endl;
}
return 0;
}
|
[["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13]]
| 1
| 378
|
#include <iostream>
#include <vector>
using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(int x, int y, vector<vector<bool>> &canGo) {
canGo[x][y] = false;
for (int i = 0; i < 9; i++)
if (canGo[x + dx[i]][y + dy[i]])
dfs(x + dx[i], y + dy[i], canGo);
return;
}
int main(void) {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
int input;
vector<vector<bool>> canGo(h + 2, vector<bool>(w + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> input;
if (input == 1) {
canGo[i][j] = true;
}
}
}
int count = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (canGo[i][j]) {
++count;
dfs(i, j, canGo);
}
}
}
cout << count << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(int x, int y, vector<vector<bool>> &canGo) {
canGo[x][y] = false;
for (int i = 0; i < 8; i++)
if (canGo[x + dx[i]][y + dy[i]])
dfs(x + dx[i], y + dy[i], canGo);
return;
}
int main(void) {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
int input;
vector<vector<bool>> canGo(h + 2, vector<bool>(w + 2, false));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> input;
if (input == 1) {
canGo[i][j] = true;
}
}
}
int count = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (canGo[i][j]) {
++count;
dfs(i, j, canGo);
}
}
}
cout << count << endl;
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 12, 13]]
| 1
| 333
|
#include <bits/stdc++.h>
using namespace std;
char a[50][50];
void calc(int i, int j) {
if (a[i][j] == '1') {
a[i][j] = 2;
calc(i + 1, j);
calc(i, j + 1);
calc(i + 1, j + 1);
calc(i + 1, j - 1);
calc(i - 1, j);
calc(i, j - 1);
calc(i - 1, j - 1);
calc(i - 1, j + 1);
} else
return;
}
int main() {
while (1) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
int count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == '1') {
count++;
calc(i, j);
}
}
}
cout << count << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[500][500];
void calc(int i, int j) {
if (a[i][j] == 1) {
a[i][j] = 2;
calc(i + 1, j);
calc(i, j + 1);
calc(i + 1, j + 1);
calc(i + 1, j - 1);
calc(i - 1, j);
calc(i, j - 1);
calc(i - 1, j - 1);
calc(i - 1, j + 1);
} else
return;
}
int main() {
while (1) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
}
}
int count = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (a[i][j] == 1) {
count++;
calc(i, j);
}
}
}
cout << count << endl;
}
return 0;
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 103, 0, 104]]
| 1
| 284
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9 + 7;
int H, W;
void dfs(int h, int w, vector<vector<int>> &table) {
// cout << h << " " << w << endl;
table[h][w] = 0;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
rep(i, 0, 8) {
int nx = w + dx[i];
int ny = h + dy[i];
if (nx >= W || nx < 0 || ny >= H || ny < 0)
continue;
if (table[ny][nx])
dfs(ny, nx, table);
}
}
main() {
cin >> W >> H;
vector<vector<int>> table;
while (W) {
table = vector<vector<int>>(H, vector<int>(W));
rep(i, 0, H) rep(j, 0, W) cin >> table[i][j];
int cnt = 0;
rep(i, 0, H) rep(j, 0, W) {
if (table[i][j] == 1) {
// cout << i << " " << j << endl;
dfs(i, j, table);
cnt++;
}
}
cout << cnt << endl;
cin >> H >> W;
}
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9 + 7;
int H, W;
void dfs(int h, int w, vector<vector<int>> &table) {
// cout << h << " " << w << endl;
table[h][w] = 0;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
rep(i, 0, 8) {
int nx = w + dx[i];
int ny = h + dy[i];
if (nx >= W || nx < 0 || ny >= H || ny < 0)
continue;
if (table[ny][nx])
dfs(ny, nx, table);
}
}
main() {
cin >> W >> H;
vector<vector<int>> table;
while (W) {
table = vector<vector<int>>(H, vector<int>(W));
rep(i, 0, H) rep(j, 0, W) cin >> table[i][j];
int cnt = 0;
rep(i, 0, H) rep(j, 0, W) {
if (table[i][j] == 1) {
// cout << i << " " << j << endl;
dfs(i, j, table);
cnt++;
}
}
cout << cnt << endl;
cin >> W >> H;
}
}
|
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 12, 22], ["-", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 152], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
| 1
| 331
|
// Tue Dec 22 22:01:34 JST 2009
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
int dx[] = {0, 1, 1, 0, -1, -1};
int dy[] = {-1, -1, 0, 1, 1, 0};
#define CONNECT true
#define UNCONNECT false
#define CANMOVE true
#define CANNOTMOVE false
class state {
public:
int len;
int x[8], y[8];
int cnt;
int path[8];
bool is_connected(int me, int you) {
if (x[me] == x[you]) {
if (y[me] - 1 == y[you] || y[me] + 1 == y[you] || y[me] == y[you])
return CONNECT;
} else if (x[me] + 1 == x[you]) {
if (y[me] == y[you] || y[me] - 1 == y[you])
return CONNECT;
} else if (x[me] - 1 == x[you]) {
if (y[me] == y[you] || y[me] + 1 == y[you])
return CONNECT;
}
return UNCONNECT;
}
bool can_good_pos(int cur) {
rep(i, cur - 1) {
if (is_connected(cur, i) == CONNECT)
return false; //
}
if (cur == 0 || is_connected(cur, cur - 1) == CONNECT)
return true; //
return false;
}
void make_next(int now, queue<state> &Q, set<state> &S, bool flag,
set<pair<int, int>> &NG) {
if (now == len) {
if (S.find((*this)) == S.end()) {
cnt++;
S.insert(*this);
Q.push(*this);
cnt--;
}
return;
}
// don't move
if (can_good_pos(now)) {
path[now] = -1;
make_next(now + 1, Q, S, CANMOVE, NG);
}
if (flag == CANNOTMOVE)
return; //
rep(i, 6) {
x[now] += dx[i];
y[now] += dy[i];
path[now] = i;
if (NG.find(mp(x[now], y[now])) == NG.end() && can_good_pos(now)) {
make_next(now + 1, Q, S, CANNOTMOVE, NG); //
}
x[now] -= dx[i];
y[now] -= dy[i];
}
}
bool operator<(const state &a) const {
rep(i, len) {
if (x[i] != a.x[i])
return x[i] < a.x[i];
if (y[i] != a.y[i])
return y[i] < a.y[i];
}
return false;
}
};
int bfs(state ini, int dx, int dy, set<pair<int, int>> &NG) {
queue<state> Q;
set<state> S;
Q.push(ini);
S.insert(ini);
while (!Q.empty()) {
state now = Q.front();
Q.pop();
if (15 < now.cnt + (max(now.x[0], dx) - min(now.x[0], dy) +
max(now.y[0], dy) - min(now.y[0], dy)))
continue;
if (now.x[0] == dx && now.y[0] == dy) {
return now.cnt;
}
now.make_next(0, Q, S, CANMOVE, NG);
}
return -1;
}
main() {
int n, m, dx, dy;
while (cin >> n && n) {
state ini;
set<pair<int, int>> NG;
ini.len = n;
ini.cnt = 0;
rep(i, n) cin >> ini.x[i] >> ini.y[i];
cin >> m;
rep(i, m) {
int f, s;
cin >> f >> s;
NG.insert(mp(f, s));
}
cin >> dx >> dy;
cout << bfs(ini, dx, dy, NG) << endl;
}
return false;
}
|
// Tue Dec 22 22:01:34 JST 2009
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
int dx[] = {0, 1, 1, 0, -1, -1};
int dy[] = {-1, -1, 0, 1, 1, 0};
#define CONNECT true
#define UNCONNECT false
#define CANMOVE true
#define CANNOTMOVE false
class state {
public:
int len;
int x[8], y[8];
int cnt;
int path[8];
bool is_connected(int me, int you) {
if (x[me] == x[you]) {
if (y[me] - 1 == y[you] || y[me] + 1 == y[you] || y[me] == y[you])
return CONNECT;
} else if (x[me] + 1 == x[you]) {
if (y[me] == y[you] || y[me] - 1 == y[you])
return CONNECT;
} else if (x[me] - 1 == x[you]) {
if (y[me] == y[you] || y[me] + 1 == y[you])
return CONNECT;
}
return UNCONNECT;
}
bool can_good_pos(int cur) {
rep(i, cur - 1) {
if (is_connected(cur, i) == CONNECT)
return false; //
}
if (cur == 0 || is_connected(cur, cur - 1) == CONNECT)
return true; //
return false;
}
void make_next(int now, queue<state> &Q, set<state> &S, bool flag,
set<pair<int, int>> &NG) {
if (now == len) {
if (S.find((*this)) == S.end()) {
cnt++;
S.insert(*this);
Q.push(*this);
cnt--;
}
return;
}
// don't move
if (can_good_pos(now)) {
path[now] = -1;
make_next(now + 1, Q, S, CANMOVE, NG);
}
if (flag == CANNOTMOVE)
return; //
rep(i, 6) {
x[now] += dx[i];
y[now] += dy[i];
path[now] = i;
if (NG.find(mp(x[now], y[now])) == NG.end() && can_good_pos(now)) {
make_next(now + 1, Q, S, CANNOTMOVE, NG); //
}
x[now] -= dx[i];
y[now] -= dy[i];
}
}
bool operator<(const state &a) const {
rep(i, len) {
if (x[i] != a.x[i])
return x[i] < a.x[i];
if (y[i] != a.y[i])
return y[i] < a.y[i];
}
return false;
}
};
int bfs(state ini, int dx, int dy, set<pair<int, int>> &NG) {
queue<state> Q;
set<state> S;
Q.push(ini);
S.insert(ini);
while (!Q.empty()) {
state now = Q.front();
Q.pop();
if (19 < now.cnt + (max(now.x[0], dx) - min(now.x[0], dy) +
max(now.y[0], dy) - min(now.y[0], dy)) /
2)
continue;
if (now.x[0] == dx && now.y[0] == dy) {
return now.cnt;
}
now.make_next(0, Q, S, CANMOVE, NG);
}
return 20;
}
main() {
int n, m, dx, dy;
while (cin >> n && n) {
state ini;
set<pair<int, int>> NG;
ini.len = n;
ini.cnt = 0;
rep(i, n) cin >> ini.x[i] >> ini.y[i];
cin >> m;
rep(i, m) {
int f, s;
cin >> f >> s;
NG.insert(mp(f, s));
}
cin >> dx >> dy;
cout << bfs(ini, dx, dy, NG) << endl;
}
return false;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 31, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 13], ["+", 15, 339, 51, 16, 12, 16, 12, 16, 17, 85], ["+", 15, 339, 51, 16, 12, 16, 12, 16, 12, 13], ["-", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13]]
| 1
| 952
|
import java.util.Scanner;
public class Main {
public static final long mod7 = 1000000007L;
static int[][] map;
static int sx, sy, ex, ey, h, w;
static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (run(sc)) {
}
}
static boolean run(Scanner sc) {
w = sc.nextInt();
h = sc.nextInt();
if (w + h == 0) {
return false;
}
map = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int v = sc.nextInt();
if (v == 2) {
sx = j;
sy = i;
map[i][j] = 0;
} else if (v == 3) {
ex = j;
ey = i;
map[i][j] = 3;
} else {
map[i][j] = v;
}
}
}
int res = rep(sx, sy, 10);
if (res == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(res);
}
return true;
}
static int rep(int px, int py, int t) {
if (t == 0) {
return Integer.MAX_VALUE;
}
int m = Integer.MAX_VALUE;
for (int di = 0; di < 4; di++) {
int[] d = dir[di];
int nx = px;
int ny = py;
int by = -1;
int bx = -1;
boolean cant = false;
for (int i = 0; i < 30; i++) {
int ty = ny + d[1];
int tx = nx + d[0];
if (ty < 0 || ty >= h || tx < 0 || tx >= w) {
break;
}
if (map[ty][tx] == 1) {
if (i == 0) {
cant = true;
break;
}
by = ty;
bx = tx;
map[by][bx] = 0;
break;
}
if (map[ty][tx] == 3) {
return 10 - t + 1;
}
ny = ty;
nx = tx;
}
if (cant) {
continue;
}
m = Math.min(m, rep(nx, ny, t - 1));
if (bx != -1) {
map[by][bx] = 1;
}
}
return m;
}
}
|
import java.util.Scanner;
public class Main {
public static final long mod7 = 1000000007L;
static int[][] map;
static int sx, sy, ex, ey, h, w;
static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (run(sc)) {
}
}
static boolean run(Scanner sc) {
w = sc.nextInt();
h = sc.nextInt();
if (w + h == 0) {
return false;
}
map = new int[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int v = sc.nextInt();
if (v == 2) {
sx = j;
sy = i;
map[i][j] = 0;
} else if (v == 3) {
ex = j;
ey = i;
map[i][j] = 3;
} else {
map[i][j] = v;
}
}
}
int res = rep(sx, sy, 10);
if (res == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(res);
}
return true;
}
static int rep(int px, int py, int t) {
if (t == 0) {
return Integer.MAX_VALUE;
}
int m = Integer.MAX_VALUE;
for (int di = 0; di < 4; di++) {
int[] d = dir[di];
int nx = px;
int ny = py;
int by = -1;
int bx = -1;
boolean cant = false;
for (int i = 0; i < 30; i++) {
int ty = ny + d[1];
int tx = nx + d[0];
if (ty < 0 || ty >= h || tx < 0 || tx >= w) {
cant = true;
break;
}
if (map[ty][tx] == 1) {
if (i == 0) {
cant = true;
break;
}
by = ty;
bx = tx;
map[by][bx] = 0;
break;
}
if (map[ty][tx] == 3) {
return 10 - t + 1;
}
ny = ty;
nx = tx;
}
if (cant) {
continue;
}
m = Math.min(m, rep(nx, ny, t - 1));
if (bx != -1) {
map[by][bx] = 1;
}
}
return m;
}
}
|
[["+", 0, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 17, 32], ["+", 0, 57, 64, 196, 0, 1, 0, 11, 12, 146], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35]]
| 3
| 586
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int w, h;
int sx, sy;
int res;
int table[30][30];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
void dfs(int x, int y, int cnt) {
if (cnt >= res)
return;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir], ny = y + dy[dir];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
else if (table[ny][nx] == 1)
continue;
else if (table[ny][nx] == 3) {
res = cnt;
return;
}
while (true) {
nx += dx[dir];
ny += dy[dir];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
break;
else if (table[ny][nx] == 1) {
table[ny][nx] = 0;
dfs(nx - dx[dir], ny - dy[dir], cnt + 1);
table[ny][nx] = 1;
break;
} else if (table[ny][nx] == 3) {
res = cnt;
return;
}
}
}
}
int main() {
while (cin >> w >> h, w) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> table[i][j];
if (table[i][j] == 2) {
sx = j;
sy = i;
}
}
}
res = 10;
dfs(sx, sy, 1);
if (res > 10)
res = -1;
cout << res << endl;
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int w, h;
int sx, sy;
int res;
int table[30][30];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
void dfs(int x, int y, int cnt) {
if (cnt >= res)
return;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir], ny = y + dy[dir];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
continue;
else if (table[ny][nx] == 1)
continue;
else if (table[ny][nx] == 3) {
res = cnt;
return;
}
while (true) {
nx += dx[dir];
ny += dy[dir];
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
break;
else if (table[ny][nx] == 1) {
table[ny][nx] = 0;
dfs(nx - dx[dir], ny - dy[dir], cnt + 1);
table[ny][nx] = 1;
break;
} else if (table[ny][nx] == 3) {
res = cnt;
return;
}
}
}
}
int main() {
while (cin >> w >> h, w) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> table[i][j];
if (table[i][j] == 2) {
sx = j;
sy = i;
}
}
}
res = 11;
dfs(sx, sy, 1);
if (res > 10)
res = -1;
cout << res << endl;
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 430
|
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF INT_MAX / 3
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define DECFOR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
#define MAX_W 20
#define MAX_H 20
#define MOVING_LIMIT 10
#define NORTH 1
#define EAST 2
#define SOUTH 3
#define WEST 4
enum CellState { EMPTY, OBSTACLE, START, STOP };
struct Position {
int x;
int y;
};
struct GameState {
int width;
int height;
CellState field[MAX_H][MAX_W];
Position pos;
int dir;
};
struct Moving {
Position begin;
Position end;
int dir;
};
int load(GameState *state) {
int w, h;
scanf("%d %d", &w, &h);
if (w == 0 && h == 0)
return 0;
state->width = w;
state->height = h;
REP(i, h) REP(j, w) {
scanf("%d", &(state->field[i][j]));
if (state->field[i][j] == START) {
state->pos.x = j;
state->pos.y = i;
}
}
return 1;
}
int move(Moving *mv, GameState *state) {
mv->begin = state->pos;
mv->dir = state->dir;
switch (state->dir) {
case NORTH:
if (state->field[state->pos.y + 1][state->pos.x] == OBSTACLE) {
return 0;
}
FOR(i, state->pos.y, state->height) {
if (state->field[i][state->pos.x] == STOP) {
state->pos.y = i;
return 2;
}
if (state->field[i][state->pos.x] == OBSTACLE) {
state->field[i][state->pos.x] = EMPTY;
state->pos.y = i - 1;
mv->end.x = state->pos.x;
mv->end.y = i;
return 1;
}
}
return 0;
case EAST:
if (state->field[state->pos.y][state->pos.x + 1] == OBSTACLE) {
return 0;
}
FOR(j, state->pos.x, state->width) {
if (state->field[state->pos.y][j] == STOP) {
state->pos.x = j;
mv->end.x = j;
mv->end.y = state->pos.y;
return 2;
}
if (state->field[state->pos.y][j] == OBSTACLE) {
state->field[state->pos.y][j] = EMPTY;
state->pos.x = j - 1;
mv->end.x = j;
mv->end.y = state->pos.y;
return 1;
}
}
return 0;
case SOUTH:
if (state->field[state->pos.y - 1][state->pos.x] == OBSTACLE) {
return 0;
}
DECFOR(i, state->pos.y, -1) {
if (state->field[i][state->pos.x] == STOP) {
state->pos.y = i;
mv->end.x = state->pos.x;
mv->end.y = i;
return 2;
}
if (state->field[i][state->pos.x] == OBSTACLE) {
state->field[i][state->pos.x] = EMPTY;
state->pos.y = i + 1;
mv->end.x = state->pos.x;
mv->end.y = i;
return 1;
}
}
return 0;
case WEST:
if (state->field[state->pos.y][state->pos.x - 1] == OBSTACLE) {
return 0;
}
DECFOR(j, state->pos.x, -1) {
if (state->field[state->pos.y][j] == STOP) {
state->pos.x = j;
return 2;
}
if (state->field[state->pos.y][j] == OBSTACLE) {
state->field[state->pos.y][j] = EMPTY;
state->pos.x = j + 1;
mv->end.x = j;
mv->end.y = state->pos.y;
return 1;
}
}
return 0;
default:
return -1;
}
}
int rotate(GameState *state) {
state->dir = (state->dir + 1) % 5;
return state->dir;
}
void revert_move(Moving *mv, GameState *state) {
state->pos = mv->begin;
state->dir = mv->dir;
state->field[mv->end.y][mv->end.x] = OBSTACLE;
}
int solve(GameState *state) {
Moving stack[MOVING_LIMIT];
int count = 0;
int ret = MOVING_LIMIT;
state->dir = NORTH;
while (1) {
switch (move(&stack[count], state)) {
case 0:
rotate(state);
break;
case 1:
if (count + 1 == MOVING_LIMIT) {
revert_move(&stack[count], state);
rotate(state);
} else {
++count;
state->dir = NORTH;
}
break;
case 2:
ret = min(ret, count);
--count;
if (count < 0)
break;
revert_move(&stack[count], state);
rotate(state);
break;
default:
--count;
if (count < 0)
break;
revert_move(&stack[count], state);
rotate(state);
break;
}
if (count < 0)
break;
}
if (ret == MOVING_LIMIT)
return -1;
return ret + 1;
}
int main() {
GameState state;
while (load(&state)) {
printf("%d\n\n", solve(&state));
}
return 0;
}
|
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF INT_MAX / 3
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define DECFOR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
#define MAX_W 20
#define MAX_H 20
#define MOVING_LIMIT 10
#define NORTH 1
#define EAST 2
#define SOUTH 3
#define WEST 4
enum CellState { EMPTY, OBSTACLE, START, STOP };
struct Position {
int x;
int y;
};
struct GameState {
int width;
int height;
CellState field[MAX_H][MAX_W];
Position pos;
int dir;
};
struct Moving {
Position begin;
Position end;
int dir;
};
int load(GameState *state) {
int w, h;
scanf("%d %d", &w, &h);
if (w == 0 && h == 0)
return 0;
state->width = w;
state->height = h;
REP(i, h) REP(j, w) {
scanf("%d", &(state->field[i][j]));
if (state->field[i][j] == START) {
state->pos.x = j;
state->pos.y = i;
}
}
return 1;
}
int move(Moving *mv, GameState *state) {
mv->begin = state->pos;
mv->dir = state->dir;
switch (state->dir) {
case NORTH:
if (state->field[state->pos.y + 1][state->pos.x] == OBSTACLE) {
return 0;
}
FOR(i, state->pos.y, state->height) {
if (state->field[i][state->pos.x] == STOP) {
state->pos.y = i;
return 2;
}
if (state->field[i][state->pos.x] == OBSTACLE) {
state->field[i][state->pos.x] = EMPTY;
state->pos.y = i - 1;
mv->end.x = state->pos.x;
mv->end.y = i;
return 1;
}
}
return 0;
case EAST:
if (state->field[state->pos.y][state->pos.x + 1] == OBSTACLE) {
return 0;
}
FOR(j, state->pos.x, state->width) {
if (state->field[state->pos.y][j] == STOP) {
state->pos.x = j;
mv->end.x = j;
mv->end.y = state->pos.y;
return 2;
}
if (state->field[state->pos.y][j] == OBSTACLE) {
state->field[state->pos.y][j] = EMPTY;
state->pos.x = j - 1;
mv->end.x = j;
mv->end.y = state->pos.y;
return 1;
}
}
return 0;
case SOUTH:
if (state->field[state->pos.y - 1][state->pos.x] == OBSTACLE) {
return 0;
}
DECFOR(i, state->pos.y, -1) {
if (state->field[i][state->pos.x] == STOP) {
state->pos.y = i;
mv->end.x = state->pos.x;
mv->end.y = i;
return 2;
}
if (state->field[i][state->pos.x] == OBSTACLE) {
state->field[i][state->pos.x] = EMPTY;
state->pos.y = i + 1;
mv->end.x = state->pos.x;
mv->end.y = i;
return 1;
}
}
return 0;
case WEST:
if (state->field[state->pos.y][state->pos.x - 1] == OBSTACLE) {
return 0;
}
DECFOR(j, state->pos.x, -1) {
if (state->field[state->pos.y][j] == STOP) {
state->pos.x = j;
return 2;
}
if (state->field[state->pos.y][j] == OBSTACLE) {
state->field[state->pos.y][j] = EMPTY;
state->pos.x = j + 1;
mv->end.x = j;
mv->end.y = state->pos.y;
return 1;
}
}
return 0;
default:
return -1;
}
}
int rotate(GameState *state) {
state->dir = (state->dir + 1) % 5;
return state->dir;
}
void revert_move(Moving *mv, GameState *state) {
state->pos = mv->begin;
state->dir = mv->dir;
state->field[mv->end.y][mv->end.x] = OBSTACLE;
}
int solve(GameState *state) {
Moving stack[MOVING_LIMIT];
int count = 0;
int ret = MOVING_LIMIT;
state->dir = NORTH;
while (1) {
switch (move(&stack[count], state)) {
case 0:
rotate(state);
break;
case 1:
if (count + 1 == MOVING_LIMIT) {
revert_move(&stack[count], state);
rotate(state);
} else {
++count;
state->dir = NORTH;
}
break;
case 2:
ret = min(ret, count);
--count;
if (count < 0)
break;
revert_move(&stack[count], state);
rotate(state);
break;
default:
--count;
if (count < 0)
break;
revert_move(&stack[count], state);
rotate(state);
break;
}
if (count < 0)
break;
}
if (ret == MOVING_LIMIT)
return -1;
return ret + 1;
}
int main() {
GameState state;
while (load(&state)) {
printf("%d\n", solve(&state));
}
return 0;
}
|
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
| 1
| 1,299
|
#include <algorithm>
#include <iostream>
using namespace std;
int w, h;
int G[100][100];
int sx, sy;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int ans;
void dfs(int px, int py, int cnt) {
if (cnt > 10)
return;
for (int i = 0; i < 4; i++) {
int nx = px + dx[i], ny = py + dy[i];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h && G[ny][nx] != 1))
continue;
while (1) {
if (G[ny][nx] == 3) {
ans = cnt;
}
nx += dx[i], ny += dy[i];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h))
break;
if (G[ny][nx] == 1) {
G[ny][nx] = 0;
dfs(nx - dx[i], ny - dy[i], cnt + 1);
G[ny][nx] = 1;
break;
}
}
}
}
int main() {
while (cin >> w >> h, w + h) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> G[i][j];
if (G[i][j] == 2) {
sx = j;
sy = i;
}
}
}
ans = 11;
dfs(sx, sy, 1);
cout << (ans > 10 ? -1 : ans) << endl;
}
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int w, h;
int G[100][100];
int sx, sy;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int ans;
void dfs(int px, int py, int cnt) {
if (cnt > 10)
return;
for (int i = 0; i < 4; i++) {
int nx = px + dx[i], ny = py + dy[i];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h && G[ny][nx] != 1))
continue;
while (1) {
if (G[ny][nx] == 3) {
ans = min(cnt, ans);
}
nx += dx[i], ny += dy[i];
if (!(0 <= nx && nx < w && 0 <= ny && ny < h))
break;
if (G[ny][nx] == 1) {
G[ny][nx] = 0;
dfs(nx - dx[i], ny - dy[i], cnt + 1);
G[ny][nx] = 1;
break;
}
}
}
}
int main() {
while (cin >> w >> h, w + h) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> G[i][j];
if (G[i][j] == 2) {
sx = j;
sy = i;
}
}
}
ans = 11;
dfs(sx, sy, 1);
cout << (ans > 10 ? -1 : ans) << endl;
}
}
|
[["+", 64, 9, 0, 1, 0, 11, 12, 2, 63, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 21], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 25]]
| 1
| 378
|
#include <math.h>
#include <stdio.h>
void main() {
int x[200], y[200];
int i, N, ni, di;
int xMax, yMax, xMin, yMin;
x[0] = y[0] = 0;
while (1) {
xMax = yMax = xMin = xMax = 0;
scanf("%d", &N);
if (N == 0)
break;
for (i = 1; i < N; i++) {
scanf("%d%d", &ni, &di);
x[i] = x[ni] - (int)(cos((90 * di) * M_PI / 180));
y[i] = y[ni] - (int)(sin((90 * di) * M_PI / 180));
}
for (i = 0; i < N; i++) {
if (xMax < x[i]) {
xMax = x[i];
} else if (xMin > x[i]) {
xMin = x[i];
}
if (yMax < y[i]) {
yMax = y[i];
} else if (yMin > y[i]) {
yMin = y[i];
}
}
printf("%d %d\n", xMax - xMin + 1, yMax - yMin + 1);
}
}
|
#include <math.h>
#include <stdio.h>
void main() {
int x[200], y[200];
int i, N, ni, di;
int xMax, yMax, xMin, yMin;
x[0] = y[0] = 0;
while (1) {
xMax = yMax = xMin = yMin = 0;
scanf("%d", &N);
if (N == 0)
break;
for (i = 1; i < N; i++) {
scanf("%d%d", &ni, &di);
x[i] = x[ni] - (int)(cos((90 * di) * M_PI / 180));
y[i] = y[ni] - (int)(sin((90 * di) * M_PI / 180));
}
for (i = 0; i < N; i++) {
if (xMax < x[i]) {
xMax = x[i];
} else if (xMin > x[i]) {
xMin = x[i];
}
if (yMax < y[i]) {
yMax = y[i];
} else if (yMin > y[i]) {
yMin = y[i];
}
}
printf("%d %d\n", xMax - xMin + 1, yMax - yMin + 1);
}
}
|
[["-", 0, 11, 12, 11, 12, 11, 12, 11, 31, 22], ["+", 0, 11, 12, 11, 12, 11, 12, 11, 31, 22]]
| 0
| 278
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class rectangle {
private:
int name, x, y;
public:
rectangle(int name, int x, int y) : name(name), x(x), y(y) {}
int getX() { return x; }
int getY() { return y; }
};
int main() {
int N;
int n, d;
int x, y;
int maxX = 0, minX = 0, maxY = 0, minY = 0;
int width = 1, height = 1;
vector<rectangle> recs;
while ((cin >> N) && (N != 0)) {
recs.push_back(rectangle(0, 0, 0)); // 0 into recs at (0,0)
if (N != 1) {
for (int i = 1; i < N; i++) {
cin >> n >> d;
x = recs[n].getX();
y = recs[n].getY();
if (d == 0) {
recs.push_back(rectangle(i, x - 1, y));
minX = min(maxX, x - 1);
} else if (d == 1) {
recs.push_back(rectangle(i, x, y - 1));
minY = min(minY, y - 1);
} else if (d == 2) {
recs.push_back(rectangle(i, x + 1, y));
maxX = max(maxX, x + 1);
} else if (d == 3) {
recs.push_back(rectangle(i, x, y + 1));
maxY = max(maxY, y + 1);
}
}
width = maxX + abs(minX) + width;
height = maxY + abs(minY) + height;
}
cout << width << " " << height << endl;
recs.clear();
maxX = 0;
minX = 0;
maxY = 0;
minY = 0;
width = 1;
height = 1;
}
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class rectangle {
private:
int name, x, y;
public:
rectangle(int name, int x, int y) : name(name), x(x), y(y) {}
int getX() { return x; }
int getY() { return y; }
};
int main() {
int N;
int n, d;
int x, y;
int maxX = 0, minX = 0, maxY = 0, minY = 0;
int width = 1, height = 1;
vector<rectangle> recs;
while ((cin >> N) && (N != 0)) {
recs.push_back(rectangle(0, 0, 0)); // 0 into recs at (0,0)
if (N != 1) {
for (int i = 1; i < N; i++) {
cin >> n >> d;
x = recs[n].getX();
y = recs[n].getY();
if (d == 0) {
recs.push_back(rectangle(i, x - 1, y));
minX = min(minX, x - 1);
} else if (d == 1) {
recs.push_back(rectangle(i, x, y - 1));
minY = min(minY, y - 1);
} else if (d == 2) {
recs.push_back(rectangle(i, x + 1, y));
maxX = max(maxX, x + 1);
} else if (d == 3) {
recs.push_back(rectangle(i, x, y + 1));
maxY = max(maxY, y + 1);
}
}
width = maxX + abs(minX) + width;
height = maxY + abs(minY) + height;
}
cout << width << " " << height << endl;
recs.clear();
maxX = 0;
minX = 0;
maxY = 0;
minY = 0;
width = 1;
height = 1;
}
return 0;
}
|
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 13], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35]]
| 1
| 415
|
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
typedef pair<int, int> P;
int main() {
int n;
while (cin >> n) {
P a[300];
a[0] = P(0, 0);
for (int i = 1; i < n; i++) {
int m, d;
cin >> m >> d;
if (d == 0) {
a[i] = P(a[m].first - 1, a[m].second);
} else if (d == 1) {
a[i] = P(a[m].first, a[m].second - 1);
} else if (d == 2) {
a[i] = P(a[m].first + 1, a[m].second);
} else {
a[i] = P(a[m].first, a[m].second + 1);
}
}
int xm = 1e-8, xs = 1e8, ym = 1e-8, ys = 18;
for (int i = 0; i < n; i++) {
xm = max(a[i].first, xm);
xs = min(xs, a[i].first);
ym = max(a[i].second, ym);
ys = min(a[i].second, ys);
}
cout << xm - xs + 1 << ' ' << ym - ys + 1 << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
typedef pair<int, int> P;
int main() {
int n;
while (cin >> n, n) {
P a[300];
a[0] = P(0, 0);
for (int i = 1; i < n; i++) {
int m, d;
cin >> m >> d;
if (d == 0) {
a[i] = P(a[m].first - 1, a[m].second);
} else if (d == 1) {
a[i] = P(a[m].first, a[m].second - 1);
} else if (d == 2) {
a[i] = P(a[m].first + 1, a[m].second);
} else {
a[i] = P(a[m].first, a[m].second + 1);
}
}
int xm = 1e-8, xs = 1e8, ym = 1e-8, ys = 18;
for (int i = 0; i < n; i++) {
xm = max(a[i].first, xm);
xs = min(xs, a[i].first);
ym = max(a[i].second, ym);
ys = min(a[i].second, ys);
}
cout << xm - xs + 1 << ' ' << ym - ys + 1 << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
| 1
| 318
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int N;
int main() {
while (cin >> N, N) {
vector<pair<int, int>> vp;
vp.push_back({0, 0});
for (int i = 1; i < N; ++i) {
int n, d;
cin >> n >> d;
int x, y;
if (d == 0) {
x = vp[n].first - 1;
y = vp[n].second;
} else if (d == 1) {
x = vp[n].first;
y = vp[n].second - 1;
} else if (d == 2) {
x = vp[n].first + 1;
y = vp[n].second;
} else if (d == 3) {
x = vp[n].first;
y = vp[n].second + 1;
}
vp.push_back({x, y});
}
int ansxu = 0, ansxd = 0, ansyl = 0, ansyr = 0;
for (int i = 0; i < vp.size(); ++i) {
ansxu = max(ansxu, vp[i].first);
if (vp[i].first < 0) {
ansxd = min(ansxd, vp[i].first);
}
ansyr = max(ansyr, vp[i].second);
if (vp[i].second < 0) {
ansyl = min(ansxd, vp[i].second);
}
// cout << vp[i].first << " " << vp[i].second << endl;
}
cout << ansxu - ansxd + 1 << " " << -ansyl + ansyr + 1 << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int N;
int main() {
while (cin >> N, N) {
vector<pair<int, int>> vp;
vp.push_back({0, 0});
for (int i = 1; i < N; ++i) {
int n, d;
cin >> n >> d;
int x, y;
if (d == 0) {
x = vp[n].first - 1;
y = vp[n].second;
} else if (d == 1) {
x = vp[n].first;
y = vp[n].second - 1;
} else if (d == 2) {
x = vp[n].first + 1;
y = vp[n].second;
} else if (d == 3) {
x = vp[n].first;
y = vp[n].second + 1;
}
vp.push_back({x, y});
}
int ansxu = 0, ansxd = 0, ansyl = 0, ansyr = 0;
for (int i = 0; i < vp.size(); ++i) {
ansxu = max(ansxu, vp[i].first);
if (vp[i].first < 0) {
ansxd = min(ansxd, vp[i].first);
}
ansyr = max(ansyr, vp[i].second);
if (vp[i].second < 0) {
ansyl = min(ansyl, vp[i].second);
}
}
cout << ansxu - ansxd + 1 << " " << -ansyl + ansyr + 1 << endl;
}
return 0;
}
|
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22]]
| 1
| 363
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
struct PLACE {
int r;
int l;
int u;
int d;
};
void solve() {
PLACE place[n + 1];
int target, cmd;
// 0を基準としそれよりもどのくらい離れているか
int maxR = 0;
int maxL = 0;
int maxU = 0;
int maxD = 0;
place[0].l = 0;
place[0].r = 0;
place[0].u = 0;
place[0].d = 0;
for (int i = 1; i < n; i++) {
cin >> target >> cmd;
switch (cmd) {
case 0: // Left
place[i].l = place[target].l + 1;
place[i].r = place[target].r - 1;
place[i].u = place[target].u;
place[i].d = place[target].d;
maxL = max(place[i].l, maxL);
break;
case 1: // Down
place[i].l = place[target].l;
place[i].r = place[target].r;
place[i].u = place[target].u - 1;
place[i].d = place[target].d + 1;
maxD = max(place[i].d, maxD);
break;
case 2: // Right
place[i].l = place[target].l - 1;
place[i].r = place[target].r = 1;
place[i].u = place[target].u;
place[i].d = place[target].d;
maxR = max(place[i].r, maxR);
break;
case 3: // Up
place[i].u = place[target].u + 1;
place[i].l = place[target].l;
place[i].r = place[target].r;
place[i].d = place[target].d;
maxU = max(place[i].u, maxU);
break;
}
}
int Hight = maxU + maxD + 1;
int Width = maxR + maxL + 1;
cout << Width << " " << Hight << endl;
}
int main() {
while (true) {
cin >> n;
if (n == 0)
break;
solve();
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
struct PLACE {
int r;
int l;
int u;
int d;
};
void solve() {
PLACE place[n + 1];
int target, cmd;
// 0を基準としそれよりもどのくらい離れているか
int maxR = 0;
int maxL = 0;
int maxU = 0;
int maxD = 0;
place[0].l = 0;
place[0].r = 0;
place[0].u = 0;
place[0].d = 0;
for (int i = 1; i < n; i++) {
cin >> target >> cmd;
switch (cmd) {
case 0: // Left
place[i].l = place[target].l + 1;
place[i].r = place[target].r - 1;
place[i].u = place[target].u;
place[i].d = place[target].d;
maxL = max(place[i].l, maxL);
break;
case 1: // Down
place[i].l = place[target].l;
place[i].r = place[target].r;
place[i].u = place[target].u - 1;
place[i].d = place[target].d + 1;
maxD = max(place[i].d, maxD);
break;
case 2: // Right
place[i].l = place[target].l - 1;
place[i].r = place[target].r + 1;
place[i].u = place[target].u;
place[i].d = place[target].d;
maxR = max(place[i].r, maxR);
break;
case 3: // Up
place[i].u = place[target].u + 1;
place[i].l = place[target].l;
place[i].r = place[target].r;
place[i].d = place[target].d - 1;
maxU = max(place[i].u, maxU);
break;
}
}
int Hight = maxU + maxD + 1;
int Width = maxR + maxL + 1;
cout << Width << " " << Hight << endl;
}
int main() {
while (true) {
cin >> n;
if (n == 0)
break;
solve();
}
return 0;
}
|
[["-", 0, 100, 0, 1, 0, 11, 12, 11, 17, 32], ["+", 0, 100, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 0, 100, 0, 1, 0, 11, 12, 16, 17, 33], ["+", 0, 100, 0, 1, 0, 11, 12, 16, 12, 13]]
| 1
| 515
|
#include <iostream>
#include <vector>
using namespace std;
struct pos {
int x;
int y;
};
int main() {
int n, n1, d1;
int left, right, up, down;
pos setpos;
vector<pos> position;
vector<pos>::iterator posit;
while (1) {
cin >> n;
if (n == 0)
break;
//初期化
left = 0;
right = 0;
up = 0;
down = 0;
setpos.x = 0;
setpos.y = 0;
position.push_back(setpos);
//入力
for (int i = 1; i < n; i++) {
cin >> n1 >> d1;
posit = position.begin() + n1;
if (d1 == 0) {
setpos.x = (*posit).x - 1;
setpos.y = (*posit).y;
if (setpos.x < left)
left = setpos.x;
position.push_back(setpos);
} else if (d1 == 1) {
setpos.x = (*posit).x;
setpos.y = (*posit).y - 1;
if (setpos.y < down)
down = setpos.y;
position.push_back(setpos);
} else if (d1 == 2) {
setpos.x = (*posit).x + 1;
setpos.y = (*posit).y;
if (right < setpos.x)
right = setpos.x;
position.push_back(setpos);
} else {
setpos.x = (*posit).x;
setpos.y = (*posit).y + 1;
if (up < setpos.y)
up = setpos.y;
position.push_back(setpos);
}
}
//表示
int maxwidth, maxheight;
left *= -1;
down *= -1;
maxwidth = left + right + 1;
maxheight = up + down + 1;
cout << maxwidth << " " << maxheight << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
struct pos {
int x;
int y;
};
int main() {
int n, n1, d1;
int left, right, up, down;
pos setpos;
vector<pos> position;
vector<pos>::iterator posit;
while (1) {
cin >> n;
if (n == 0)
break;
//初期化
left = 0;
right = 0;
up = 0;
down = 0;
setpos.x = 0;
setpos.y = 0;
position.clear();
position.push_back(setpos);
//入力
for (int i = 1; i < n; i++) {
cin >> n1 >> d1;
posit = position.begin() + n1;
if (d1 == 0) {
setpos.x = (*posit).x - 1;
setpos.y = (*posit).y;
if (setpos.x < left)
left = setpos.x;
position.push_back(setpos);
} else if (d1 == 1) {
setpos.x = (*posit).x;
setpos.y = (*posit).y - 1;
if (setpos.y < down)
down = setpos.y;
position.push_back(setpos);
} else if (d1 == 2) {
setpos.x = (*posit).x + 1;
setpos.y = (*posit).y;
if (right < setpos.x)
right = setpos.x;
position.push_back(setpos);
} else {
setpos.x = (*posit).x;
setpos.y = (*posit).y + 1;
if (up < setpos.y)
up = setpos.y;
position.push_back(setpos);
}
}
//表示
int maxwidth, maxheight;
left *= -1;
down *= -1;
maxwidth = left + right + 1;
maxheight = up + down + 1;
cout << maxwidth << " " << maxheight << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131]]
| 1
| 399
|
dx,dy = [-1,0,1,0],[0,-1,0,1]
while True:
N = eval(input())
if N == 0: break
x,y = [0]*N,[0]*N
for i in range(N-1):
n,d = list(map(int,input().split()))
x[i] = x[n] + dx[d]
y[i] = y[n] + dy[d]
print(max(x) - min(x) + 1, max(y) - min(y) + 1)
|
dx,dy = [-1,0,1,0],[0,-1,0,1]
while True:
N = eval(input())
if N == 0: break
x,y = [0]*N,[0]*N
for i in range(1,N):
n,d = list(map(int,input().split()))
x[i] = x[n] + dx[d]
y[i] = y[n] + dy[d]
print(max(x) - min(x) + 1, max(y) - min(y) + 1)
|
[["-", 0, 7, 12, 652, 3, 4, 0, 657, 31, 22], ["-", 0, 7, 12, 652, 3, 4, 0, 657, 17, 33], ["+", 8, 196, 0, 7, 12, 652, 3, 4, 0, 21], ["+", 8, 196, 0, 7, 12, 652, 3, 4, 0, 22]]
| 5
| 140
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main {
static PrintWriter out;
static InputReader ir;
static void solve() {
for (;;) {
int w = ir.nextInt();
int h = ir.nextInt();
if (w == 0 && h == 0)
return;
G g = new G(w * h, true);
for (int i = 0; i < 2 * h - 1; i++) {
int row = i / 2;
if (i % 2 == 0) {
int[] a = ir.nextIntArray(w - 1);
for (int j = 0; j < w - 1; j++) {
if (a[j] == 0) {
g.addEdge(w * row + j, w * row + j + 1, 1);
}
}
} else {
int[] a = ir.nextIntArray(w);
for (int j = 0; j < w; j++) {
if (a[j] == 0) {
g.addEdge(w * row + j, w * (row + 1) + j, 1);
}
}
}
}
int[] d = g.dijkstra(0);
out.println((d[w * h - 1] >= 1 << 26) ? 0 : d[w * h - 1]);
}
}
static class G {
AL[] g, rg;
private int V;
private boolean ndir;
public G(int V, boolean ndir) {
this.V = V;
this.ndir = ndir;
g = new AL[V];
for (int i = 0; i < V; i++)
g[i] = new AL();
}
public void addEdge(int u, int v, int t) {
g[u].add(new int[] {v, t});
if (this.ndir)
g[v].add(new int[] {u, t});
}
public void addEdge(int u, int v) { addEdge(u, v, 0); }
public int to(int from, int ind) { return g[from].get(ind)[0]; }
public int cost(int from, int ind) { return g[from].get(ind)[1]; }
public int size(int from) { return g[from].size(); }
public int[] dijkstra(int s) {
int[] dist = new int[this.V];
java.util.PriorityQueue<int[]> pque =
new java.util.PriorityQueue<int[]>(11, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
});
Arrays.fill(dist, 1 << 26);
dist[s] = 0;
pque.offer(new int[] {0, s});
while (!pque.isEmpty()) {
int[] p = pque.poll();
int v = p[1];
if (dist[v] < p[0])
continue;
for (int i = 0; i < g[v].size(); i++) {
int to = to(v, i), cost = cost(v, i);
if (dist[to] > dist[v] + cost) {
dist[to] = dist[v] + cost;
pque.offer(new int[] {dist[to], to});
}
}
}
return dist;
}
public int[] tporder() {
boolean[] vis = new boolean[V];
ArrayList<Integer> ord = new ArrayList<>();
for (int i = 0; i < V; i++)
if (!vis[i])
ts(i, vis, ord);
int[] ret = new int[V];
for (int i = ord.size() - 1; i >= 0; i--)
ret[ord.size() - 1 - i] = ord.get(i);
return ret;
}
public int[] scc() {
rg = new AL[V];
for (int i = 0; i < V; i++)
rg[i] = new AL();
int from, to;
for (int i = 0; i < V; i++) {
for (int j = 0; j < g[i].size(); j++) {
to = i;
from = to(i, j);
rg[from].add(new int[] {to, 0});
}
}
int[] ord = tporder();
int k = 0;
boolean[] vis = new boolean[V];
int[] ret = new int[V + 1];
for (int i = 0; i < V; i++)
if (!vis[i])
rs(ord[i], vis, ret, k++);
ret[V] = k;
return ret;
}
private void ts(int now, boolean[] vis, ArrayList<Integer> ord) {
vis[now] = true;
int to;
for (int i = 0; i < g[now].size(); i++) {
to = to(now, i);
if (!vis[to])
ts(to, vis, ord);
}
ord.add(now);
}
private void rs(int now, boolean[] vis, int[] ret, int k) {
vis[now] = true;
ret[now] = k;
int to;
for (int i = 0; i < rg[now].size(); i++) {
to = rg[now].get(i)[0];
if (!vis[to])
rs(to, vis, ret, k);
}
}
static class AL extends ArrayList<int[]> {};
}
public static void main(String[] args) throws Exception {
ir = new InputReader(System.in);
out = new PrintWriter(System.out);
solve();
out.flush();
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() { return Double.parseDouble(next()); }
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
|
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main {
static PrintWriter out;
static InputReader ir;
static void solve() {
for (;;) {
int w = ir.nextInt();
int h = ir.nextInt();
if (w == 0 && h == 0)
return;
G g = new G(w * h, true);
for (int i = 0; i < 2 * h - 1; i++) {
int row = i / 2;
if (i % 2 == 0) {
int[] a = ir.nextIntArray(w - 1);
for (int j = 0; j < w - 1; j++) {
if (a[j] == 0) {
g.addEdge(w * row + j, w * row + j + 1, 1);
}
}
} else {
int[] a = ir.nextIntArray(w);
for (int j = 0; j < w; j++) {
if (a[j] == 0) {
g.addEdge(w * row + j, w * (row + 1) + j, 1);
}
}
}
}
int[] d = g.dijkstra(0);
out.println((d[w * h - 1] >= 1 << 26) ? 0 : d[w * h - 1] + 1);
}
}
static class G {
AL[] g, rg;
private int V;
private boolean ndir;
public G(int V, boolean ndir) {
this.V = V;
this.ndir = ndir;
g = new AL[V];
for (int i = 0; i < V; i++)
g[i] = new AL();
}
public void addEdge(int u, int v, int t) {
g[u].add(new int[] {v, t});
if (this.ndir)
g[v].add(new int[] {u, t});
}
public void addEdge(int u, int v) { addEdge(u, v, 0); }
public int to(int from, int ind) { return g[from].get(ind)[0]; }
public int cost(int from, int ind) { return g[from].get(ind)[1]; }
public int size(int from) { return g[from].size(); }
public int[] dijkstra(int s) {
int[] dist = new int[this.V];
java.util.PriorityQueue<int[]> pque =
new java.util.PriorityQueue<int[]>(11, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
});
Arrays.fill(dist, 1 << 26);
dist[s] = 0;
pque.offer(new int[] {0, s});
while (!pque.isEmpty()) {
int[] p = pque.poll();
int v = p[1];
if (dist[v] < p[0])
continue;
for (int i = 0; i < g[v].size(); i++) {
int to = to(v, i), cost = cost(v, i);
if (dist[to] > dist[v] + cost) {
dist[to] = dist[v] + cost;
pque.offer(new int[] {dist[to], to});
}
}
}
return dist;
}
public int[] tporder() {
boolean[] vis = new boolean[V];
ArrayList<Integer> ord = new ArrayList<>();
for (int i = 0; i < V; i++)
if (!vis[i])
ts(i, vis, ord);
int[] ret = new int[V];
for (int i = ord.size() - 1; i >= 0; i--)
ret[ord.size() - 1 - i] = ord.get(i);
return ret;
}
public int[] scc() {
rg = new AL[V];
for (int i = 0; i < V; i++)
rg[i] = new AL();
int from, to;
for (int i = 0; i < V; i++) {
for (int j = 0; j < g[i].size(); j++) {
to = i;
from = to(i, j);
rg[from].add(new int[] {to, 0});
}
}
int[] ord = tporder();
int k = 0;
boolean[] vis = new boolean[V];
int[] ret = new int[V + 1];
for (int i = 0; i < V; i++)
if (!vis[i])
rs(ord[i], vis, ret, k++);
ret[V] = k;
return ret;
}
private void ts(int now, boolean[] vis, ArrayList<Integer> ord) {
vis[now] = true;
int to;
for (int i = 0; i < g[now].size(); i++) {
to = to(now, i);
if (!vis[to])
ts(to, vis, ord);
}
ord.add(now);
}
private void rs(int now, boolean[] vis, int[] ret, int k) {
vis[now] = true;
ret[now] = k;
int to;
for (int i = 0; i < rg[now].size(); i++) {
to = rg[now].get(i)[0];
if (!vis[to])
rs(to, vis, ret, k);
}
}
static class AL extends ArrayList<int[]> {};
}
public static void main(String[] args) throws Exception {
ir = new InputReader(System.in);
out = new PrintWriter(System.out);
solve();
out.flush();
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() { return Double.parseDouble(next()); }
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
}
|
[["+", 0, 492, 3, 4, 0, 510, 75, 16, 17, 72], ["+", 0, 492, 3, 4, 0, 510, 75, 16, 12, 499]]
| 3
| 2,066
|
from copy import deepcopy
while True:
w, h = map(int, input().split())
if w == 0:
break
wwal = [] # |
hwal = [] # ー
a = [0] * w
b = []
for i in range(2 * h - 1):
if i % 2 == 0:
wwal.append(list(map(int, input().split())))
c = deepcopy(a)
b.append(c)
else:
hwal.append(list(map(int, input().split())))
b[0][0] = 1
start = [[0, 0]]
flag = 0
while flag == 0:
flag = 1
stamemo = []
for i in range(len(start)):
t = start.pop()
# Left
if t[1] != 0:
if wwal[t[0]][t[1] - 1] == 0 and b[t[0]][t[1]-1] == 0:
b[t[0]][t[1] - 1] = b[t[0]][t[1]] + 1
stamemo.append([t[0], t[1] - 1])
flag = 0
# righT
if t[1] != w-1:
if wwal[t[0]][t[1]] == 0 and b[t[0]][t[1]+1] == 0:
b[t[0]][t[1] + 1] = b[t[0]][t[1]] + 1
if t[0] == h - 1 and t[1] + 1 == w - 1:
print(b[t[0]][t[1] + 1])
flag = 2
break
stamemo.append([t[0], t[1]+1])
flag = 0
# Up
if t[0] != 0:
if hwal[t[0] - 1][t[1]] == 0 and b[t[0] - 1][t[1]] == 0:
b[t[0] - 1][t[1]] = b[t[0]][t[1]] + 1
stamemo.append([t[0] - 1, t[1]])
flag = 0
# dowN
if t[0] != h-1:
if hwal[t[0]][t[1]] == 0 and b[t[0] + 1][t[1]] == 0:
b[t[0] + 1][t[1]] = b[t[0]][t[1]] + 1
if t[0]+1 == h - 1 and t[1] == w - 1:
print(b[t[0]+1][t[1]])
flag = 2
break
stamemo.append([t[0] + 1, t[1]])
flag = 0
start = stamemo
print(start)
if flag == 1:
print(0)
|
from copy import deepcopy
while True:
w, h = map(int, input().split())
if w == 0:
break
wwal = [] # |
hwal = [] # ー
a = [0] * w
b = []
for i in range(2 * h - 1):
if i % 2 == 0:
wwal.append(list(map(int, input().split())))
c = deepcopy(a)
b.append(c)
else:
hwal.append(list(map(int, input().split())))
b[0][0] = 1
start = [[0, 0]]
flag = 0
while flag == 0:
flag = 1
stamemo = []
for i in range(len(start)):
t = start.pop()
# Left
if t[1] != 0:
if wwal[t[0]][t[1] - 1] == 0 and b[t[0]][t[1]-1] == 0:
b[t[0]][t[1] - 1] = b[t[0]][t[1]] + 1
stamemo.append([t[0], t[1] - 1])
flag = 0
# righT
if t[1] != w-1:
if wwal[t[0]][t[1]] == 0 and b[t[0]][t[1]+1] == 0:
b[t[0]][t[1] + 1] = b[t[0]][t[1]] + 1
if t[0] == h - 1 and t[1] + 1 == w - 1:
print(b[t[0]][t[1] + 1])
flag = 2
break
stamemo.append([t[0], t[1]+1])
flag = 0
# Up
if t[0] != 0:
if hwal[t[0] - 1][t[1]] == 0 and b[t[0] - 1][t[1]] == 0:
b[t[0] - 1][t[1]] = b[t[0]][t[1]] + 1
stamemo.append([t[0] - 1, t[1]])
flag = 0
# dowN
if t[0] != h-1:
if hwal[t[0]][t[1]] == 0 and b[t[0] + 1][t[1]] == 0:
b[t[0] + 1][t[1]] = b[t[0]][t[1]] + 1
if t[0]+1 == h - 1 and t[1] == w - 1:
print(b[t[0]+1][t[1]])
flag = 2
break
stamemo.append([t[0] + 1, t[1]])
flag = 0
start = stamemo
if flag == 1:
print(0)
|
[["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 668
|
def bfs(maze):
d = [[0] * 70 for i in range(70)]
d[1][1] = 1
que = [(1, 1)]
while 0 < len(que):
new_que = []
for x, y in que:
if x == 2 * w - 1 and y == 2 * h - 1:
return d[y][x]
for dx, dy in ((0, 1), (-1, 0), (0, -1), (1, 0)):
mx, my = x + dx, y + dy
nx, ny = mx + dx, my + dy
if nx < 0 and w < nx and ny < 0 and h < ny:
continue
if maze[my][mx] == 0 and d[ny][nx] == 0:
new_que.append((nx, ny))
d[ny][nx] = d[y][x] + 1
que = new_que
return 0
while True:
w, h = map(int, input().split())
maze = [[0] * (w * 2) for i in range(h * 2)]
if w == h == 0:
break
for x in range(w * 2 + 1):
maze[0][x] = maze[h * 2][x] = 1
for y in range(h * 2 + 1):
maze[y][0] = maze[y][w * 2] = 1
for y in range(1, h * 2):
a = [int(i) for i in input().split()]
if y % 2 == 1:
for x in range(w - 1):
maze[y][2 * x + 2] = a[x]
else:
for x in range(w):
maze[y][2 * x + 1] = a[x]
print(bfs(maze))
|
def bfs(maze):
d = [[0] * 70 for i in range(70)]
d[1][1] = 1
que = [(1, 1)]
while 0 < len(que):
new_que = []
for x, y in que:
if x == 2 * w - 1 and y == 2 * h - 1:
return d[y][x]
for dx, dy in ((0, 1), (-1, 0), (0, -1), (1, 0)):
mx, my = x + dx, y + dy
nx, ny = mx + dx, my + dy
if nx < 0 and w < nx and ny < 0 and h < ny:
continue
if maze[my][mx] == 0 and d[ny][nx] == 0:
new_que.append((nx, ny))
d[ny][nx] = d[y][x] + 1
que = new_que
return 0
while True:
w, h = map(int, input().split())
maze = [[0] * (w * 2 + 1) for i in range(h * 2 + 1)]
if w == h == 0:
break
for x in range(w * 2 + 1):
maze[0][x] = maze[h * 2][x] = 1
for y in range(h * 2 + 1):
maze[y][0] = maze[y][w * 2] = 1
for y in range(1, h * 2):
a = [int(i) for i in input().split()]
if y % 2 == 1:
for x in range(w - 1):
maze[y][2 * x + 2] = a[x]
else:
for x in range(w):
maze[y][2 * x + 1] = a[x]
print(bfs(maze))
|
[["+", 12, 658, 8, 657, 12, 23, 0, 657, 17, 72], ["+", 12, 658, 8, 657, 12, 23, 0, 657, 12, 612], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 17, 72], ["+", 0, 659, 12, 652, 3, 4, 0, 657, 12, 612]]
| 5
| 417
|
from collections import deque
while True:
W,H = map(int, input().split())
if W == 0: break
es = [[] for i in range(W*H)]
for y in range(H):
walls_h = map(int, input().split())
for x in range(W-1):
if walls_h[x]: continue
es[y*W + x].append(y*W + x+1)
es[y*W + x+1].append(y*W + x)
if y == H-1: break
walls_v = map(int, input().split())
for x in range(W):
if walls_v[x]: continue
es[y*W + x].append((y+1)*W + x)
es[(y+1)*W + x].append(y*W + x)
q = deque([0])
dist = [0] * (W*H)
dist[0] = 1
while q:
fr = q.popleft()
for to in es[fr]:
if dist[to] == 0:
dist[to] = dist[fr] + 1
q.append(to)
print(dist[-1])
|
from collections import deque
while True:
W,H = map(int, input().split())
if W == 0: break
es = [[] for i in range(W*H)]
for y in range(H):
walls_h = list(map(int, input().split()))
for x in range(W-1):
if walls_h[x]: continue
es[y*W + x].append(y*W + x+1)
es[y*W + x+1].append(y*W + x)
if y == H-1: break
walls_v = list(map(int, input().split()))
for x in range(W):
if walls_v[x]: continue
es[y*W + x].append((y+1)*W + x)
es[(y+1)*W + x].append(y*W + x)
q = deque([0])
dist = [0] * (W*H)
dist[0] = 1
while q:
fr = q.popleft()
for to in es[fr]:
if dist[to] == 0:
dist[to] = dist[fr] + 1
q.append(to)
print(dist[-1])
|
[["+", 8, 196, 0, 1, 0, 662, 12, 652, 63, 22], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 24], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 25]]
| 5
| 276
|
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000 - 1
int main() {
int n, i, input;
int tn, tmp;
int tetra_n[181];
int *tetra_sum, *tetra_sum_odd;
char a[10];
for (n = 0; n <= 180; n++)
tetra_n[n] = n * (n + 1) * (n + 2) / 6;
tetra_sum = (int *)malloc(sizeof(int) * (MAX + 1));
tetra_sum_odd = (int *)malloc(sizeof(int) * (MAX + 1));
tetra_sum[0] = 0;
tetra_sum_odd[0] = 0;
for (i = 1; i <= MAX; i++) {
tetra_sum[i] = MAX;
tetra_sum_odd[i] = MAX;
}
for (n = 1; n <= 180; n++) {
tn = tetra_n[n];
if (tn % 2 == 0) {
for (i = tn; i <= MAX; i++) {
tmp = tetra_sum[i - tn] + 1;
if (tmp < tetra_sum[i])
tetra_sum[i] = tmp;
}
} else {
for (i = tn; i <= MAX; i++) {
tmp = tetra_sum[i - tn] + 1;
if (tmp < tetra_sum[i])
tetra_sum[i] = tmp;
tmp = tetra_sum_odd[i - tn] + 1;
if (tmp < tetra_sum_odd[i])
tetra_sum_odd[i] = tmp;
}
}
}
while (1) {
input = atof(fgets(a, sizeof(a) / sizeof(a[0]), stdin));
if (input == 0)
break;
printf("%d %d\n", tetra_sum[input], tetra_sum_odd[input]);
}
free(tetra_sum);
free(tetra_sum);
return 0;
}
|
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000000 - 1
int main() {
int n, i, input;
int tn, tmp;
int tetra_n[181];
int *tetra_sum, *tetra_sum_odd;
char a[10];
for (n = 0; n <= 180; n++)
tetra_n[n] = n * (n + 1) * (n + 2) / 6;
tetra_sum = (int *)malloc(sizeof(int) * (MAX + 1));
tetra_sum_odd = (int *)malloc(sizeof(int) * (MAX + 1));
tetra_sum[0] = 0;
tetra_sum_odd[0] = 0;
for (i = 1; i <= MAX; i++) {
tetra_sum[i] = MAX;
tetra_sum_odd[i] = MAX;
}
for (n = 1; n <= 180; n++) {
tn = tetra_n[n];
if (tn % 2 == 0) {
for (i = tn; i <= MAX; i++) {
tmp = tetra_sum[i - tn] + 1;
if (tmp < tetra_sum[i])
tetra_sum[i] = tmp;
}
} else {
for (i = tn; i <= MAX; i++) {
tmp = tetra_sum[i - tn] + 1;
if (tmp < tetra_sum[i])
tetra_sum[i] = tmp;
tmp = tetra_sum_odd[i - tn] + 1;
if (tmp < tetra_sum_odd[i])
tetra_sum_odd[i] = tmp;
}
}
}
while (1) {
input = atof(fgets(a, sizeof(a) / sizeof(a[0]), stdin));
if (input == 0)
break;
printf("%d %d\n", tetra_sum[input], tetra_sum_odd[input]);
}
free(tetra_sum);
free(tetra_sum_odd);
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
| 0
| 377
|
#include <stdio.h>
#define min(a, b) a < b ? a : b
int main() {
int n;
int i, j;
int c;
int a[1000001];
int b[1000001];
for (i = 0; i < 1000001; i++) {
a[i] = i;
b[i] = i;
}
for (i = 3, c = 4; i < 1000001; i++) {
for (j = c; j < 1000001; j++) {
a[j] = min(a[j], a[j - c] + 1);
}
if (c % 2) {
for (j = a; j < 1000001; j++) {
b[j] = min(b[j], b[j - c] + 1);
}
}
c = i * (i + 1) * (i + 2) / 6;
}
while (scanf("%d", &n), n) {
printf("%d %d\n", a[n], b[n]);
}
return 0;
}
|
#include <stdio.h>
#define min(a, b) a < b ? a : b
int main() {
int n;
int i, j;
int c;
int a[1000001];
int b[1000001];
for (i = 0; i < 1000001; i++) {
a[i] = i;
b[i] = i;
}
for (i = 3, c = 4; c < 1000001; i++) {
for (j = c; j < 1000001; j++) {
a[j] = min(a[j], a[j - c] + 1);
}
if (c % 2) {
for (j = c; j < 1000001; j++) {
b[j] = min(b[j], b[j - c] + 1);
}
}
c = i * (i + 1) * (i + 2) / 6;
}
while (scanf("%d", &n), n) {
printf("%d %d\n", a[n], b[n]);
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 31, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 31, 22], ["-", 0, 57, 64, 9, 0, 7, 10, 11, 12, 22], ["+", 0, 57, 64, 9, 0, 7, 10, 11, 12, 22]]
| 0
| 224
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int INF = (1LL << 30) - 1;
int MOD = 1e9 + 7;
int MAX_NUM = 1000000;
main() {
vector<int> V, W;
for (int i = 1; i * (i + 1) * (i + 2) / 6 <= MAX_NUM; i++) {
int num = i * (i + 1) * (i + 2) / 6;
V.push_back(num);
if (i % 4 == 1)
W.push_back(num);
}
vector<int> t1(MAX_NUM, INF), t2(MAX_NUM, INF);
t1[0] = t2[0] = 0;
rep(i, 0, V.size()) {
rep(j, 0, MAX_NUM) {
if (j + V[i] > MAX_NUM)
break;
t1[j + V[i]] = min(t1[j + V[i]], t1[j] + 1);
}
}
rep(i, 0, W.size()) {
rep(j, 0, MAX_NUM) {
if (j + W[i] > MAX_NUM)
break;
t2[j + W[i]] = min(t2[j + V[i]], t2[j] + 1);
}
}
int N;
cin >> N;
while (N) {
cout << t1[N] << " " << t2[N] << endl;
cin >> N;
}
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int INF = (1LL << 30) - 1;
int MOD = 1e9 + 7;
int MAX_NUM = 1000000;
main() {
vector<int> V, W;
for (int i = 1; i * (i + 1) * (i + 2) / 6 <= MAX_NUM; i++) {
int num = i * (i + 1) * (i + 2) / 6;
V.push_back(num);
if (i % 4 == 1)
W.push_back(num);
}
vector<int> t1(MAX_NUM, INF), t2(MAX_NUM, INF);
t1[0] = t2[0] = 0;
rep(i, 0, V.size()) {
rep(j, 0, MAX_NUM) {
if (j + V[i] > MAX_NUM)
break;
t1[j + V[i]] = min(t1[j + V[i]], t1[j] + 1);
}
}
rep(i, 0, W.size()) {
rep(j, 0, MAX_NUM) {
if (j + W[i] > MAX_NUM)
break;
t2[j + W[i]] = min(t2[j + W[i]], t2[j] + 1);
}
}
int N;
cin >> N;
while (N) {
cout << t1[N] << " " << t2[N] << endl;
cin >> N;
}
}
|
[["-", 0, 69, 341, 342, 0, 16, 12, 69, 28, 22], ["+", 0, 69, 341, 342, 0, 16, 12, 69, 28, 22]]
| 1
| 340
|
#include <bits/stdc++.h>
using namespace std;
int all[1000001];
int odd[1000001];
int const INF = 1 << 29;
int main() {
fill(all, all + 1000001, INF);
fill(odd, odd + 1000001, INF);
all[0] = odd[0] = 0;
for (int i = 1;; i++) {
int n = i * (i + 1) * (i + 2);
if (n > 1000000)
break;
for (int j = n; j <= 1000000; j++) {
all[j] = min(all[j - n] + 1, all[j]);
if (n % 2) {
odd[j] = min(odd[j - n] + 1, odd[j]);
}
}
}
int N;
while (cin >> N && N) {
cout << all[N] << ' ' << odd[N] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int all[1000001];
int odd[1000001];
int const INF = 1 << 29;
int main() {
fill(all, all + 1000001, INF);
fill(odd, odd + 1000001, INF);
all[0] = odd[0] = 0;
for (int i = 1;; i++) {
int n = i * (i + 1) * (i + 2) / 6;
if (n > 1000000)
break;
for (int j = n; j <= 1000000; j++) {
all[j] = min(all[j - n] + 1, all[j]);
if (n % 2) {
odd[j] = min(odd[j - n] + 1, odd[j]);
}
}
}
int N;
while (cin >> N && N) {
cout << all[N] << ' ' << odd[N] << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 43, 49, 50, 51, 16, 17, 85], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13]]
| 1
| 206
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000
using namespace std;
typedef list<int> L;
typedef pair<int, int> P;
typedef vector<int> V;
typedef queue<int> Q;
typedef stack<int> S;
typedef map<string, int> M;
int N;
int dp[1000001];
int dp2[1000001];
int tetra[1000];
int main() {
for (int i = 0; i <= INF; i++) {
dp[i] = INF;
dp2[i] = INF;
}
int n = 1;
int tmp;
while ((tmp = (n * (n + 1) * (n + 2) / 6)) <= 1000000) {
tetra[n++] = tmp;
dp[tmp] = 1;
if (tmp % 2)
dp2[tmp] = 1;
}
int max = 1;
while (cin >> N, N) {
if (max > N) {
cout << dp[N] << " ";
} else {
for (int i = max; i <= N; i++) {
for (int j = 1; tetra[j] <= i; j++) {
dp[i] = min(dp[i], dp[i - tetra[j]] + 1);
}
}
cout << dp[N] << " ";
}
if (max > N) {
cout << dp2[N] << endl;
} else {
for (int i = 1; i <= N; i++) {
for (int j = 1; tetra[j] <= i; j++) {
if (tetra[j] % 2)
dp2[i] = min(dp2[i], dp2[i - tetra[j]] + 1);
}
}
cout << dp[N] << endl;
max = N;
}
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000
using namespace std;
typedef list<int> L;
typedef pair<int, int> P;
typedef vector<int> V;
typedef queue<int> Q;
typedef stack<int> S;
typedef map<string, int> M;
int N;
int dp[1000001];
int dp2[1000001];
int tetra[1000];
int main() {
for (int i = 0; i <= INF; i++) {
dp[i] = INF;
dp2[i] = INF;
}
int n = 1;
int tmp;
while ((tmp = (n * (n + 1) * (n + 2) / 6)) <= 1000000) {
tetra[n++] = tmp;
dp[tmp] = 1;
if (tmp % 2)
dp2[tmp] = 1;
}
int max = 1;
while (cin >> N, N) {
if (max > N) {
cout << dp[N] << " ";
} else {
for (int i = max; i <= N; i++) {
for (int j = 1; tetra[j] <= i; j++) {
dp[i] = min(dp[i], dp[i - tetra[j]] + 1);
}
}
cout << dp[N] << " ";
}
if (max > N) {
cout << dp2[N] << endl;
} else {
for (int i = max; i <= N; i++) {
for (int j = 1; tetra[j] <= i; j++) {
if (tetra[j] % 2)
dp2[i] = min(dp2[i], dp2[i - tetra[j]] + 1);
}
}
cout << dp2[N] << endl;
max = N;
}
}
return 0;
}
|
[["-", 0, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 0, 9, 0, 7, 10, 43, 49, 50, 51, 22], ["-", 0, 1, 0, 16, 31, 16, 12, 69, 28, 22], ["+", 0, 1, 0, 16, 31, 16, 12, 69, 28, 22]]
| 1
| 407
|
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
void solve(int h, int w) {
vector<vector<int>> v(h * w);
rep(i, h) {
rep(j, w - 1) {
int x;
cin >> x;
if (!x) {
v[i * w + j].push_back(i * w + j + 1);
v[i * w + j + 1].push_back(i * w + j);
}
}
if (i == h - 1)
break;
rep(j, w) {
int x;
cin >> x;
if (!x) {
v[i * w + j].push_back((i + 1) * w + j);
v[(i + 1) * w + j].push_back(i * w + j);
}
}
}
vector<int> dist(h * w, inf);
dist[0] = 1;
queue<int> que;
que.push(0);
while (que.size()) {
int cur = que.front();
que.pop();
for (auto to : v[cur]) {
if (dist[to] > dist[cur] + 1) {
dist[to] = dist[cur] + 1;
que.push(to);
}
}
}
if (dist[h * w - 1] == inf)
cout << -1 << endl;
else
cout << dist[h * w - 1] << endl;
}
int main() {
int w, h;
while (cin >> w >> h, w != 0)
solve(h, w);
return 0;
}
|
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
void solve(int h, int w) {
vector<vector<int>> v(h * w);
rep(i, h) {
rep(j, w - 1) {
int x;
cin >> x;
if (!x) {
v[i * w + j].push_back(i * w + j + 1);
v[i * w + j + 1].push_back(i * w + j);
}
}
if (i == h - 1)
break;
rep(j, w) {
int x;
cin >> x;
if (!x) {
v[i * w + j].push_back((i + 1) * w + j);
v[(i + 1) * w + j].push_back(i * w + j);
}
}
}
vector<int> dist(h * w, inf);
dist[0] = 1;
queue<int> que;
que.push(0);
while (que.size()) {
int cur = que.front();
que.pop();
for (auto to : v[cur]) {
if (dist[to] > dist[cur] + 1) {
dist[to] = dist[cur] + 1;
que.push(to);
}
}
}
if (dist[h * w - 1] == inf)
cout << 0 << endl;
else
cout << dist[h * w - 1] << endl;
}
int main() {
int w, h;
while (cin >> w >> h, w != 0)
solve(h, w);
return 0;
}
|
[["-", 0, 57, 64, 1, 0, 16, 31, 16, 12, 13], ["+", 0, 57, 64, 1, 0, 16, 31, 16, 12, 13]]
| 1
| 429
|
#include <algorithm>
#include <cstdio>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int main() {
for (int h, w; scanf("%d%d", &h, &w), h;) {
int hor[50][50], ver[50][50];
rep(i, h) {
rep(j, w - 1) scanf("%d", ver[i] + j);
if (i < h - 1) {
rep(j, w) scanf("%d", hor[i] + j);
}
}
int d[50][50];
rep(i, h) rep(j, w) d[i][j] = INF;
d[0][0] = 0;
rep(_, h * w) {
rep(i, h) rep(j, w) rep(k, 4) {
int y = i + dy[k], x = j + dx[k];
if (k == 0 && ver[i][j] == 0 || k == 1 && hor[i + 1][j] == 0 ||
k == 2 && ver[i][j - 1] == 0 || k == 3 && hor[i][j] == 0)
d[y][x] = min(d[y][x], d[i][j] + 1);
}
}
printf("%d\n", d[h - 1][w - 1] < INF ? d[h - 1][w - 1] : 0);
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int main() {
for (int h, w; scanf("%d%d", &w, &h), h;) {
int hor[50][50], ver[50][50];
rep(i, h) {
rep(j, w - 1) scanf("%d", ver[i] + j);
if (i < h - 1) {
rep(j, w) scanf("%d", hor[i] + j);
}
}
int d[50][50];
rep(i, h) rep(j, w) d[i][j] = INF;
d[0][0] = 1;
rep(_, h * w) {
rep(i, h) rep(j, w) rep(k, 4) {
int y = i + dy[k], x = j + dx[k];
if (k == 0 && ver[i][j] == 0 || k == 1 && hor[i - 1][j] == 0 ||
k == 2 && ver[i][j - 1] == 0 || k == 3 && hor[i][j] == 0)
d[y][x] = min(d[y][x], d[i][j] + 1);
}
}
printf("%d\n", d[h - 1][w - 1] < INF ? d[h - 1][w - 1] : 0);
}
return 0;
}
|
[["-", 15, 34, 31, 2, 3, 4, 0, 66, 28, 22], ["+", 15, 34, 31, 2, 3, 4, 0, 66, 28, 22], ["-", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["-", 31, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 33]]
| 1
| 384
|
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define BUFFER_SIZE 1024
int bfs(int sx, int sy);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct queue {
int buffer[BUFFER_SIZE];
int head;
int tail;
};
void init(struct queue *q) {
q->head = 0;
q->tail = -1;
}
void enqueue(struct queue *q, int x) {
if (q->head == q->tail) {
fprintf(stderr, "queue is full\n");
exit(1);
}
q->buffer[q->head] = x;
q->head = (q->head + 1) % BUFFER_SIZE;
}
int dequeue(struct queue *q) {
int t = (q->tail + 1) % BUFFER_SIZE;
if (q->head == t) {
fprintf(stderr, "queue is empty\n");
exit(1);
}
q->tail = t;
return q->buffer[q->tail];
}
int map_d[32][32];
int map_a[32][32];
int map_b[32][32];
int w, h;
bool isOK(int sx, int sy, int nx, int ny) {
if (ny < 0 || nx < 0)
return false;
if (sx == nx) {
if (sy < ny) {
if (map_a[sx][sy] == 0)
return true;
} else if (sy > ny) {
if (map_a[sx][ny] == 0)
return true;
}
} else {
if (sx < nx) {
if (map_b[sx][sy] == 0)
return true;
} else {
if (map_b[nx][sy] == 0)
return true;
}
}
return false;
}
//---->x
//|
//|
// y
int main() {
for (;;) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
map_d[i][j] = 100000;
map_a[i][j] = 1;
map_b[i][j] = 1;
}
}
map_d[0][0] = 0;
scanf("%d %d", &w, &h);
if (w == 0)
return 0;
//??\???
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < w - 1; j++) {
scanf("%d", &map_a[i][j]);
}
for (int j = 0; j < w; j++) {
scanf("%d", &map_b[i][j]);
}
}
for (int j = 0; j < w - 1; j++) {
scanf("%d", &map_a[h - 1][j]);
}
bfs(0, 0);
if (map_d[h - 1][w - 1] = 100000)
printf("0\n");
else
printf("%d\n", map_d[h - 1][w - 1] + 1);
}
}
int bfs(int sx, int sy) {
struct queue q;
init(&q);
enqueue(&q, sx);
enqueue(&q, sy);
while (q.head != (q.tail + 1) % BUFFER_SIZE) {
int x, y;
x = dequeue(&q);
y = dequeue(&q);
if (x == w - 1 && y == h - 1)
return 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (!isOK(x, y, nx, ny) || map_d[nx][ny] != 100000) {
continue;
}
if (map_d[nx][ny] > map_d[x][y] + 1)
map_d[nx][ny] = map_d[x][y] + 1;
enqueue(&q, nx);
enqueue(&q, ny);
}
}
}
|
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define BUFFER_SIZE 1024
int bfs(int sx, int sy);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct queue {
int buffer[BUFFER_SIZE];
int head;
int tail;
};
void init(struct queue *q) {
q->head = 0;
q->tail = -1;
}
void enqueue(struct queue *q, int x) {
if (q->head == q->tail) {
fprintf(stderr, "queue is full\n");
exit(1);
}
q->buffer[q->head] = x;
q->head = (q->head + 1) % BUFFER_SIZE;
}
int dequeue(struct queue *q) {
int t = (q->tail + 1) % BUFFER_SIZE;
if (q->head == t) {
fprintf(stderr, "queue is empty\n");
exit(1);
}
q->tail = t;
return q->buffer[q->tail];
}
int map_d[32][32];
int map_a[32][32];
int map_b[32][32];
int w, h;
bool isOK(int sx, int sy, int nx, int ny) {
if (ny < 0 || nx < 0)
return false;
if (sx == nx) {
if (sy < ny) {
if (map_a[sx][sy] == 0)
return true;
} else if (sy > ny) {
if (map_a[sx][ny] == 0)
return true;
}
} else {
if (sx < nx) {
if (map_b[sx][sy] == 0)
return true;
} else {
if (map_b[nx][sy] == 0)
return true;
}
}
return false;
}
//---->x
//|
//|
// y
int main() {
for (;;) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
map_d[i][j] = 100000;
map_a[i][j] = 1;
map_b[i][j] = 1;
}
}
map_d[0][0] = 0;
scanf("%d %d", &w, &h);
if (w == 0)
return 0;
//??\???
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < w - 1; j++) {
scanf("%d", &map_a[i][j]);
}
for (int j = 0; j < w; j++) {
scanf("%d", &map_b[i][j]);
}
}
for (int j = 0; j < w - 1; j++) {
scanf("%d", &map_a[h - 1][j]);
}
bfs(0, 0);
if (map_d[h - 1][w - 1] == 100000)
printf("0\n");
else
printf("%d\n", map_d[h - 1][w - 1] + 1);
}
}
int bfs(int sx, int sy) {
struct queue q;
init(&q);
enqueue(&q, sx);
enqueue(&q, sy);
while (q.head != (q.tail + 1) % BUFFER_SIZE) {
int x, y;
x = dequeue(&q);
y = dequeue(&q);
if (x == w - 1 && y == h - 1)
return 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (!isOK(x, y, nx, ny) || map_d[nx][ny] != 100000) {
continue;
}
if (map_d[nx][ny] > map_d[x][y] + 1)
map_d[nx][ny] = map_d[x][y] + 1;
enqueue(&q, nx);
enqueue(&q, ny);
}
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 11, 17, 32], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 60]]
| 1
| 877
|
#include <iostream>
#include <queue>
#include <vector>
#define INF (1 << 29)
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {-1, 0, 1, 0};
int main() {
int H, W, T;
vector<vector<vector<bool>>> M;
while (true) {
cin >> W >> H;
if (W == 0 && H == 0) {
break;
}
M = vector<vector<vector<bool>>>(
H, vector<vector<bool>>(W, vector<bool>(4, false)));
for (int i = 0; i < W; i++) {
M[0][i][0] = true;
}
for (int i = 0; i < H; i++) {
M[i][W - 1][1] = true;
}
for (int i = 0; i < W; i++) {
M[H - 1][i][2] = true;
}
for (int i = 0; i < H; i++) {
M[i][0][3] = true;
}
for (int i = 0; i < 2 * H - 1; i++) {
for (int j = 0; j < (i % 2 == 0 ? W - 1 : W); j++) {
cin >> T;
if (T == 1) {
if (i % 2 == 0) {
M[i / 2][j][1] = true;
M[i / 2][j + 1][3] = true;
} else {
M[i / 2][j][2] = true;
M[i / 2 + 1][j][0] = true;
}
}
}
}
vector<vector<int>> dp(H, vector<int>(W, INF));
dp[0][0] = 0;
queue<pair<int, int>> que;
que.push(make_pair(0, 0));
while (!que.empty()) {
pair<int, int> p1 = que.front();
que.pop();
int x = p1.first;
int y = p1.second;
for (int dir = 0; dir < 4; dir++) {
if (M[y][x][dir] == false) {
if (dp[y + dy[dir]][x + dx[dir]] == INF) {
dp[y + dy[dir]][x + dx[dir]] = dp[y][x] + 1;
que.push(make_pair(x + dx[dir], y + dy[dir]));
}
}
}
}
if (dp[H - 1][W - 1] == INF) {
cout << 0 << endl;
} else {
cout << dp[H - 1][W - 1] << endl;
}
}
return 0;
}
|
#include <iostream>
#include <queue>
#include <vector>
#define INF (1 << 29)
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {-1, 0, 1, 0};
int main() {
int H, W, T;
vector<vector<vector<bool>>> M;
while (true) {
cin >> W >> H;
if (W == 0 && H == 0) {
break;
}
M = vector<vector<vector<bool>>>(
H, vector<vector<bool>>(W, vector<bool>(4, false)));
for (int i = 0; i < W; i++) {
M[0][i][0] = true;
}
for (int i = 0; i < H; i++) {
M[i][W - 1][1] = true;
}
for (int i = 0; i < W; i++) {
M[H - 1][i][2] = true;
}
for (int i = 0; i < H; i++) {
M[i][0][3] = true;
}
for (int i = 0; i < 2 * H - 1; i++) {
for (int j = 0; j < (i % 2 == 0 ? W - 1 : W); j++) {
cin >> T;
if (T == 1) {
if (i % 2 == 0) {
M[i / 2][j][1] = true;
M[i / 2][j + 1][3] = true;
} else {
M[i / 2][j][2] = true;
M[i / 2 + 1][j][0] = true;
}
}
}
}
vector<vector<int>> dp(H, vector<int>(W, INF));
dp[0][0] = 1;
queue<pair<int, int>> que;
que.push(make_pair(0, 0));
while (!que.empty()) {
pair<int, int> p1 = que.front();
que.pop();
int x = p1.first;
int y = p1.second;
for (int dir = 0; dir < 4; dir++) {
if (M[y][x][dir] == false) {
if (dp[y + dy[dir]][x + dx[dir]] == INF) {
dp[y + dy[dir]][x + dx[dir]] = dp[y][x] + 1;
que.push(make_pair(x + dx[dir], y + dy[dir]));
}
}
}
}
if (dp[H - 1][W - 1] == INF) {
cout << 0 << endl;
} else {
cout << dp[H - 1][W - 1] << endl;
}
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
| 1
| 645
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
vector<vector<int>> tatekabe(h, vector<int>(w - 1, 0));
vector<vector<int>> yokokabe(h - 1, vector<int>(w, 0));
vector<vector<int>> f(w, vector<int>(h, 0));
for (int i = 0; i < 2 * h - 1; i++) {
if (i % 2 == 0) {
for (int j = 0; j < w - 1; j++) {
cin >> tatekabe[i / 2][j];
}
} else {
for (int j = 0; j < w; j++) {
cin >> yokokabe[i / 2][j];
}
}
}
int ans = 1;
queue<pair<int, int>> q;
q.push({0, 0});
f[0][0] = 1;
while (true) {
if (q.empty() == true) {
cout << 0 << endl;
break;
}
unsigned int cnt = q.size();
while (cnt != 0) {
pair<int, int> temp = q.front();
q.pop();
cnt--;
if (temp.first == w - 1 && temp.second == h - 1) {
cout << ans << endl;
break;
}
for (int i = 0; i < 4; i++) {
bool flag = true;
int nx = temp.first + dx[i];
int ny = temp.second + dy[i];
if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
continue;
}
switch (i) {
case 0:
if (tatekabe[temp.second][temp.first] == 1) {
flag = false;
}
break;
case 1:
if (yokokabe[temp.second][temp.first] == 1) {
flag = false;
}
break;
case 2:
if (tatekabe[temp.second][temp.first - 1] == 1) {
flag = false;
}
break;
case 3:
if (yokokabe[temp.second - 1][temp.first] == 1) {
flag = false;
}
break;
}
if (flag == true && f[nx][ny] == 0) {
q.push({nx, ny});
f[nx][ny] = 1;
}
}
}
ans++;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
while (true) {
int w, h;
cin >> w >> h;
if (w == 0 && h == 0) {
break;
}
vector<vector<int>> tatekabe(h, vector<int>(w - 1, 0));
vector<vector<int>> yokokabe(h - 1, vector<int>(w, 0));
vector<vector<int>> f(w, vector<int>(h, 0));
for (int i = 0; i < 2 * h - 1; i++) {
if (i % 2 == 0) {
for (int j = 0; j < w - 1; j++) {
cin >> tatekabe[i / 2][j];
}
} else {
for (int j = 0; j < w; j++) {
cin >> yokokabe[i / 2][j];
}
}
}
int ans = 1;
queue<pair<int, int>> q;
q.push({0, 0});
f[0][0] = 1;
while (true) {
if (q.empty() == true) {
cout << 0 << endl;
break;
}
unsigned int cnt = q.size();
while (cnt != 0) {
pair<int, int> temp = q.front();
q.pop();
cnt--;
if (temp.first == w - 1 && temp.second == h - 1) {
cout << ans << endl;
goto End;
}
for (int i = 0; i < 4; i++) {
bool flag = true;
int nx = temp.first + dx[i];
int ny = temp.second + dy[i];
if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
continue;
}
switch (i) {
case 0:
if (tatekabe[temp.second][temp.first] == 1) {
flag = false;
}
break;
case 1:
if (yokokabe[temp.second][temp.first] == 1) {
flag = false;
}
break;
case 2:
if (tatekabe[temp.second][temp.first - 1] == 1) {
flag = false;
}
break;
case 3:
if (yokokabe[temp.second - 1][temp.first] == 1) {
flag = false;
}
break;
}
if (flag == true && f[nx][ny] == 0) {
q.push({nx, ny});
f[nx][ny] = 1;
}
}
}
ans++;
}
End:;
}
return 0;
}
|
[["-", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 126, 0, 127], ["+", 8, 9, 0, 57, 64, 9, 0, 126, 128, 129], ["+", 8, 9, 0, 52, 8, 9, 0, 97, 128, 129], ["+", 8, 9, 0, 52, 8, 9, 0, 97, 0, 102], ["+", 0, 52, 8, 9, 0, 97, 0, 1, 0, 35]]
| 1
| 604
|
#include <iostream>
#include <queue>
using namespace std;
int main() {
int w, h;
int map
[30]
[30]; //?£???????,???,???,??????????????????????????????1,2,4,8????????????
while (cin >> w >> h && w > 0 && h > 0) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
map[i][j] = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i == 0)
map[i][j] |= 1;
else {
int wall;
cin >> wall;
if (wall == 1) {
map[i - 1][j] |= 8;
map[i][j] |= 1;
}
}
if (i == h - 1)
map[i][j] |= 8;
}
map[i][0] |= 2;
for (int j = 1; j < w; j++) {
int wall;
cin >> wall;
if (wall == 1) {
map[i][j - 1] |= 4;
map[i][j] |= 2;
}
}
map[i][w - 1] |= 4;
}
int d[30][30];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
d[i][j] = 0;
d[0][0] = 1;
queue<int> cur;
cur.push(0 * w + 0);
int sh[4] = {-w, -1, 1, w};
while (!cur.empty()) {
for (int i = 0; i < 4; i++) {
if ((map[cur.front() / w][cur.front() % w] & (1 << i)) == 0 &&
d[(cur.front() + sh[i]) / w][(cur.front() + sh[i]) % w] == 0) {
d[(cur.front() + sh[i]) / w][(cur.front() + sh[i]) % w] =
d[cur.front() / w][cur.front() % w] + 1;
cur.push(cur.front() + sh[i]);
}
}
cur.pop();
}
cout << d[h - 1][w - 1] << endl;
return 0;
}
}
|
#include <iostream>
#include <queue>
using namespace std;
int main() {
int w, h;
int map
[30]
[30]; //?£???????,???,???,??????????????????????????????1,2,4,8????????????
while (cin >> w >> h && w > 0 && h > 0) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
map[i][j] = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i == 0)
map[i][j] |= 1;
else {
int wall;
cin >> wall;
if (wall == 1) {
map[i - 1][j] |= 8;
map[i][j] |= 1;
}
}
if (i == h - 1)
map[i][j] |= 8;
}
map[i][0] |= 2;
for (int j = 1; j < w; j++) {
int wall;
cin >> wall;
if (wall == 1) {
map[i][j - 1] |= 4;
map[i][j] |= 2;
}
}
map[i][w - 1] |= 4;
}
int d[30][30];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
d[i][j] = 0;
d[0][0] = 1;
queue<int> cur;
cur.push(0 * w + 0);
int sh[4] = {-w, -1, 1, w};
while (!cur.empty()) {
for (int i = 0; i < 4; i++) {
if ((map[cur.front() / w][cur.front() % w] & (1 << i)) == 0 &&
d[(cur.front() + sh[i]) / w][(cur.front() + sh[i]) % w] == 0) {
d[(cur.front() + sh[i]) / w][(cur.front() + sh[i]) % w] =
d[cur.front() / w][cur.front() % w] + 1;
cur.push(cur.front() + sh[i]);
}
}
cur.pop();
}
cout << d[h - 1][w - 1] << endl;
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
| 1
| 551
|
#include <iostream>
#include <queue>
using namespace std;
int w, h, a, ans, tx, ty;
bool map[62][62];
int lap[62][62];
struct Node {
int x, y, d;
Node(int a, int b, int c) {
x = a;
y = b;
d = c;
}
};
typedef struct Node node;
int main() {
//??\????????????????????§??????????????????????????¨????????????????????????????????????
cin >> w;
cin >> h;
while (w != 0 || h != 0) {
ans = 0;
for (int lop = 0; lop < 62; lop++) {
for (int lop2 = 0; lop2 < 62; lop2++) {
if (lop == 0 || lop == 61 || lop2 == 0 || lop2 == 61 || lop2 == w * 2 ||
lop == h * 2) {
map[lop][lop2] = 1;
} else {
map[lop][lop2] = 0;
}
lap[lop][lop2] = 0;
}
}
//?????????????????´
map[0][1] = 0;
map[h * 2][w * 2 - 1] = 0;
//??\?????£?????£
for (int lop = 0; lop < h * 2 - 1; lop++) {
if (lop % 2 == 0 || lop == 0) {
for (int lop2 = 0; lop2 < w - 1; lop2++) {
cin >> a;
if (a == 1) {
if (map[lop][lop2 * 2 + 2] == 0) {
map[lop][lop2 * 2 + 2] = 1;
}
if (map[lop + 1][lop2 * 2 + 2] == 0) {
map[lop + 1][lop2 * 2 + 2] = 1;
}
if (map[lop + 2][lop2 * 2 + 2] == 0) {
map[lop + 2][lop2 * 2 + 2] = 1;
}
}
}
} else {
for (int lop2 = 0; lop2 < w; lop2++) {
cin >> a;
if (a == 1) {
if (map[lop + 1][lop2 * 2] == 0) {
map[lop + 1][lop2 * 2] = 1;
}
if (map[lop + 1][lop2 * 2 + 1] == 0) {
map[lop + 1][lop2 * 2 + 1] = 1;
}
if (map[lop + 1][lop2 * 2 + 2] == 0) {
map[lop + 1][lop2 * 2 + 2] = 1;
}
}
}
}
}
//?£???????????????????????????????????£?????????????????????????
//?????¨?¨???§??????????????´???????£???????
bool done[62][62] = {0};
queue<node> q;
q.push(node(1, 1, 0));
//??¨??¢?´¢??¨?????\?????????
while (q.size() != 0) {
node qq = q.front();
q.pop();
//???????????? ??????????????¶ 1,1,0
if (done[qq.y][qq.x] == 0) {
//?£??????????????????????
done[qq.y][qq.x] = 1;
//??????????????????????£??????????
if (qq.x == w * 2 - 1 && qq.y == h * 2 - 1) {
ans = (qq.d + 2) / 2;
break;
}
//??´????????????????????¢
// cout<<"aaaa"<<endl;
if (map[qq.y][qq.x - 1] == 0) {
q.push(node(qq.x - 1, qq.y, qq.d + 1));
}
if (map[qq.y - 1][qq.x] == 0) {
q.push(node(qq.x, qq.y - 1, qq.d + 1));
}
if (map[qq.y][qq.x + 1] == 0) {
q.push(node(qq.x + 1, qq.y, qq.d + 1));
}
if (map[qq.y + 1][qq.x] == 0) {
q.push(node(qq.x, qq.y + 1, qq.d + 1));
}
}
//?????????????????´???0??????????????????
}
cout << ans / 2 << endl;
cin >> w;
cin >> h;
}
return 0;
}
|
#include <iostream>
#include <queue>
using namespace std;
int w, h, a, ans, tx, ty;
bool map[62][62];
int lap[62][62];
struct Node {
int x, y, d;
Node(int a, int b, int c) {
x = a;
y = b;
d = c;
}
};
typedef struct Node node;
int main() {
//??\????????????????????§??????????????????????????¨????????????????????????????????????
cin >> w;
cin >> h;
while (w != 0 || h != 0) {
ans = 0;
for (int lop = 0; lop < 62; lop++) {
for (int lop2 = 0; lop2 < 62; lop2++) {
if (lop == 0 || lop == 61 || lop2 == 0 || lop2 == 61 || lop2 == w * 2 ||
lop == h * 2) {
map[lop][lop2] = 1;
} else {
map[lop][lop2] = 0;
}
lap[lop][lop2] = 0;
}
}
//?????????????????´
map[0][1] = 0;
map[h * 2][w * 2 - 1] = 0;
//??\?????£?????£
for (int lop = 0; lop < h * 2 - 1; lop++) {
if (lop % 2 == 0 || lop == 0) {
for (int lop2 = 0; lop2 < w - 1; lop2++) {
cin >> a;
if (a == 1) {
if (map[lop][lop2 * 2 + 2] == 0) {
map[lop][lop2 * 2 + 2] = 1;
}
if (map[lop + 1][lop2 * 2 + 2] == 0) {
map[lop + 1][lop2 * 2 + 2] = 1;
}
if (map[lop + 2][lop2 * 2 + 2] == 0) {
map[lop + 2][lop2 * 2 + 2] = 1;
}
}
}
} else {
for (int lop2 = 0; lop2 < w; lop2++) {
cin >> a;
if (a == 1) {
if (map[lop + 1][lop2 * 2] == 0) {
map[lop + 1][lop2 * 2] = 1;
}
if (map[lop + 1][lop2 * 2 + 1] == 0) {
map[lop + 1][lop2 * 2 + 1] = 1;
}
if (map[lop + 1][lop2 * 2 + 2] == 0) {
map[lop + 1][lop2 * 2 + 2] = 1;
}
}
}
}
}
//?£???????????????????????????????????£?????????????????????????
//?????¨?¨???§??????????????´???????£???????
bool done[62][62] = {0};
queue<node> q;
q.push(node(1, 1, 0));
//??¨??¢?´¢??¨?????\?????????
while (q.size() != 0) {
node qq = q.front();
q.pop();
//???????????? ??????????????¶ 1,1,0
if (done[qq.y][qq.x] == 0) {
//?£??????????????????????
done[qq.y][qq.x] = 1;
//??????????????????????£??????????
if (qq.x == w * 2 - 1 && qq.y == h * 2 - 1) {
ans = (qq.d + 2) / 2;
break;
}
//??´????????????????????¢
// cout<<"aaaa"<<endl;
if (map[qq.y][qq.x - 1] == 0) {
q.push(node(qq.x - 1, qq.y, qq.d + 1));
}
if (map[qq.y - 1][qq.x] == 0) {
q.push(node(qq.x, qq.y - 1, qq.d + 1));
}
if (map[qq.y][qq.x + 1] == 0) {
q.push(node(qq.x + 1, qq.y, qq.d + 1));
}
if (map[qq.y + 1][qq.x] == 0) {
q.push(node(qq.x, qq.y + 1, qq.d + 1));
}
}
//?????????????????´???0??????????????????
}
cout << ans << endl;
cin >> w;
cin >> h;
}
return 0;
}
|
[["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 85], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
| 1
| 874
|
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000
// aizu 200???
//??±???????????¢?´¢??§??????????????????
//???????????????????????¢?´¢??§????????????
int h, w;
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
int main() {
while (1) {
cin >> w >> h;
if (w == 0)
break;
vector<vector<bool>> canGo(2 * h + 2, vector<bool>(2 * w + 2, true));
for (int i = 1; i <= 2 * h - 1; i++) {
for (int j = 1; j <= 2 * w - 1; j++) {
canGo[i][j] = false;
}
}
//??\???
for (int i = 1; i <= 2 * h - 1; i++) {
if (i % 2 == 1) {
for (int j = 2; j <= 2 * w - 1; j += 2) {
bool input;
cin >> input;
canGo[i][j] = input;
}
} else {
for (int j = 1; j <= 2 * w - 1; j += 2) {
bool input;
cin >> input;
canGo[i][j] = input;
}
}
}
//??\??????????????¢?´¢
queue<pair<int, pair<int, int>>> wait;
wait.push(make_pair(0, make_pair(1, 1)));
canGo[1][1] = true;
//??\???????????????
int ans = 0; // ans???0??§?????????
while (!wait.empty()) { //?????\???????????????????????§
//?????\????????????????´????????????????
int nowcost = wait.front().first;
int nowi = wait.front().second.first;
int nowj = wait.front().second.second;
// cout << nowcost << " " << nowi << " " << nowj << endl;
wait.pop();
//??´??????????????????????????????????????????????????????
if (nowi == 2 * h - 1 && nowj == 2 * w - 1) {
ans = nowcost;
break;
}
//??????????????¢?´¢
for (int k = 0; k < 4; k++) {
int nexti = nowi + 2 * di[k];
int nextj = nowj + 2 * dj[k];
//??????????????????????????????
if (canGo[nowi + di[k]][nowj + dj[k]] == 0 &&
canGo[nexti][nextj] == 0) {
//????????????????????\???????????????
//??????????´¢???????????¨?????????????£???§?????????
wait.push(make_pair(nowcost + 1, make_pair(nexti, nextj)));
canGo[nexti][nextj] = true;
}
}
}
if (ans == 0)
cout << ans;
else
cout << ans + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000
// aizu 200???
//??±???????????¢?´¢??§??????????????????
//???????????????????????¢?´¢??§????????????
int h, w;
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
int main() {
while (1) {
cin >> w >> h;
if (w == 0)
break;
vector<vector<bool>> canGo(2 * h + 2, vector<bool>(2 * w + 2, true));
for (int i = 1; i <= 2 * h - 1; i++) {
for (int j = 1; j <= 2 * w - 1; j++) {
canGo[i][j] = false;
}
}
//??\???
for (int i = 1; i <= 2 * h - 1; i++) {
if (i % 2 == 1) {
for (int j = 2; j <= 2 * w - 1; j += 2) {
bool input;
cin >> input;
canGo[i][j] = input;
}
} else {
for (int j = 1; j <= 2 * w - 1; j += 2) {
bool input;
cin >> input;
canGo[i][j] = input;
}
}
}
//??\??????????????¢?´¢
queue<pair<int, pair<int, int>>> wait;
wait.push(make_pair(0, make_pair(1, 1)));
canGo[1][1] = true;
//??\???????????????
int ans = 0; // ans???0??§?????????
while (!wait.empty()) { //?????\???????????????????????§
//?????\????????????????´????????????????
int nowcost = wait.front().first;
int nowi = wait.front().second.first;
int nowj = wait.front().second.second;
// cout << nowcost << " " << nowi << " " << nowj << endl;
wait.pop();
//??´??????????????????????????????????????????????????????
if (nowi == 2 * h - 1 && nowj == 2 * w - 1) {
ans = nowcost;
break;
}
//??????????????¢?´¢
for (int k = 0; k < 4; k++) {
int nexti = nowi + 2 * di[k];
int nextj = nowj + 2 * dj[k];
//??????????????????????????????
if (canGo[nowi + di[k]][nowj + dj[k]] == 0 &&
canGo[nexti][nextj] == 0) {
//????????????????????\???????????????
//??????????´¢???????????¨?????????????£???§?????????
wait.push(make_pair(nowcost + 1, make_pair(nexti, nextj)));
canGo[nexti][nextj] = true;
}
}
}
if (ans == 0)
cout << ans << endl;
else
cout << ans + 1 << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 57, 64, 1, 0, 16, 17, 151], ["+", 8, 9, 0, 57, 64, 1, 0, 16, 12, 22]]
| 1
| 529
|
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
typedef pair<int, int> P;
bool wall[30][30][4];
int dis[30][30];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
memset(wall, 0, sizeof(wall));
memset(dis, 0, sizeof(dis));
for (int i = 0; i < (m * 2) - 1; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n - 1; j++) {
int w;
cin >> w;
if (w == 1) {
wall[i / 2][j][0] = 1;
wall[i / 2][j + 1][2] = 1;
}
}
} else {
for (int j = 0; j < n; j++) {
int w;
cin >> w;
if (w == 1) {
wall[i / 2][j][3] = 1;
wall[(i / 2) + 1][j][1] = 1;
}
}
}
}
queue<P> que;
que.push(P(0, 0));
dis[0][0] = 1;
while (que.size()) {
P p = que.front();
que.pop();
if (p.first == (n - 1) && p.second == (m - 1))
break;
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m && dis[ny][nx] == 0 &&
wall[p.second][p.first][i] == 0) {
que.push(P(nx, ny));
dis[ny][nx] = dis[p.second][p.first] + 1;
}
}
}
if (dis[m - 1][n - 1] == 0) {
cout << -1 << endl;
} else {
cout << dis[m - 1][n - 1] << endl;
}
}
return 0;
}
|
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
typedef pair<int, int> P;
bool wall[30][30][4];
int dis[30][30];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
int main() {
while (true) {
int n, m;
cin >> n >> m;
if (n == 0 && m == 0)
break;
memset(wall, 0, sizeof(wall));
memset(dis, 0, sizeof(dis));
for (int i = 0; i < (m * 2) - 1; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n - 1; j++) {
int w;
cin >> w;
if (w == 1) {
wall[i / 2][j][0] = 1;
wall[i / 2][j + 1][2] = 1;
}
}
} else {
for (int j = 0; j < n; j++) {
int w;
cin >> w;
if (w == 1) {
wall[i / 2][j][3] = 1;
wall[(i / 2) + 1][j][1] = 1;
}
}
}
}
queue<P> que;
que.push(P(0, 0));
dis[0][0] = 1;
while (que.size()) {
P p = que.front();
que.pop();
if (p.first == (n - 1) && p.second == (m - 1))
break;
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m && dis[ny][nx] == 0 &&
wall[p.second][p.first][i] == 0) {
que.push(P(nx, ny));
dis[ny][nx] = dis[p.second][p.first] + 1;
}
}
}
if (dis[m - 1][n - 1] == 0) {
cout << 0 << endl;
} else {
cout << dis[m - 1][n - 1] << endl;
}
}
return 0;
}
|
[["-", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 64, 9, 0, 1, 0, 16, 31, 16, 12, 13]]
| 1
| 538
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n - 1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
struct L {
P a, b;
L(const P &a, const P &b) : a(a), b(b) {}
};
struct C {
P p;
double r;
C(const P p, double r) : p(p), r(r) {}
};
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
bool LLcross(L l1, L l2) {
return abs(cross(l1.b - l1.a, l2.b - l2.a)) > EPS ||
abs(cross(l1.b - l1.a, l2.b - l1.a)) < EPS;
}
double dist(P p1, P p2) {
return sqrt((p2.real() - p1.real()) * (p2.real() - p1.real()) +
(p2.imag() - p1.imag()) * (p2.imag() - p1.imag()));
}
const int UE = 1;
const int MIGI = 2;
const int SHITA = 4;
const int HIDARI = 8;
const int CHECK = 16;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dk[] = {UE, MIGI, SHITA, HIDARI};
struct state {
int x, y, t;
state(int x, int y, int t) : x(x), y(y), t(t){};
};
int main() {
int w, h;
while (cin >> w >> h, w) {
vvi field(h, vi(w));
REP(i, w) {
field[0][i] += UE;
field[h - 1][i] += SHITA;
}
REP(i, h) {
field[i][0] += HIDARI;
field[i][w - 1] += MIGI;
}
int tmp;
REP(i, 2 * h - 1) {
if (i % 2 == 0) {
REP(j, w - 1) {
cin >> tmp;
if (tmp) {
field[i / 2][j] += MIGI;
field[i / 2][j + 1] += HIDARI;
}
}
} else {
REP(j, w) {
cin >> tmp;
if (tmp) {
field[i / 2][j] += SHITA;
field[i / 2 + 1][j] += UE;
}
}
}
}
queue<state> Q;
Q.push(state(0, 0, 1));
field[0][0] += CHECK;
int ans = -1;
while (!Q.empty()) {
state s = Q.front();
Q.pop();
if (s.x == h - 1 && s.y == w - 1) {
ans = s.t;
goto out;
}
REP(d, 4) {
if (field[s.x][s.y] & dk[d]) {
} else {
if (!(field[s.x + dx[d]][s.y + dy[d]] & CHECK)) {
Q.push(state(s.x + dx[d], s.y + dy[d], s.t + 1));
field[s.x + dx[d]][s.y + dy[d]] += CHECK;
}
}
}
}
out:
cout << ans << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T> inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n - 1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
#include <complex>
typedef complex<double> P;
struct L {
P a, b;
L(const P &a, const P &b) : a(a), b(b) {}
};
struct C {
P p;
double r;
C(const P p, double r) : p(p), r(r) {}
};
double dot(P a, P b) { return real(conj(a) * b); }
double cross(P a, P b) { return imag(conj(a) * b); }
bool LLcross(L l1, L l2) {
return abs(cross(l1.b - l1.a, l2.b - l2.a)) > EPS ||
abs(cross(l1.b - l1.a, l2.b - l1.a)) < EPS;
}
double dist(P p1, P p2) {
return sqrt((p2.real() - p1.real()) * (p2.real() - p1.real()) +
(p2.imag() - p1.imag()) * (p2.imag() - p1.imag()));
}
const int UE = 1;
const int MIGI = 2;
const int SHITA = 4;
const int HIDARI = 8;
const int CHECK = 16;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dk[] = {UE, MIGI, SHITA, HIDARI};
struct state {
int x, y, t;
state(int x, int y, int t) : x(x), y(y), t(t){};
};
int main() {
int w, h;
while (cin >> w >> h, w) {
vvi field(h, vi(w));
REP(i, w) {
field[0][i] += UE;
field[h - 1][i] += SHITA;
}
REP(i, h) {
field[i][0] += HIDARI;
field[i][w - 1] += MIGI;
}
int tmp;
REP(i, 2 * h - 1) {
if (i % 2 == 0) {
REP(j, w - 1) {
cin >> tmp;
if (tmp) {
field[i / 2][j] += MIGI;
field[i / 2][j + 1] += HIDARI;
}
}
} else {
REP(j, w) {
cin >> tmp;
if (tmp) {
field[i / 2][j] += SHITA;
field[i / 2 + 1][j] += UE;
}
}
}
}
queue<state> Q;
Q.push(state(0, 0, 1));
field[0][0] += CHECK;
int ans = 0;
while (!Q.empty()) {
state s = Q.front();
Q.pop();
if (s.x == h - 1 && s.y == w - 1) {
ans = s.t;
goto out;
}
REP(d, 4) {
if (field[s.x][s.y] & dk[d]) {
} else {
if (!(field[s.x + dx[d]][s.y + dy[d]] & CHECK)) {
Q.push(state(s.x + dx[d], s.y + dy[d], s.t + 1));
field[s.x + dx[d]][s.y + dy[d]] += CHECK;
}
}
}
}
out:
cout << ans << endl;
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 994
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
class T {
public:
int x, y, ans;
T(int x, int y, int ans) : x(x), y(y), ans(ans) {}
};
int main() {
int w, h;
A:
cin >> w >> h;
if (w == 0)
return 0;
int t[31][31] = {0};
int z[31][31] = {0};
rep(i, h) {
rep(j, w - 1) { cin >> t[j][i]; }
if (i != h - 1)
rep(j, w) { cin >> z[j][i]; }
}
/*
rep(i,h){
rep(j,w)printf("%d ",t[i][j]);puts("");
rep(j,w)printf("%d ",y[i][j]);puts("");
puts("");
}*/
queue<T> que;
que.push(T(0, 0, 0));
int visit[31][31] = {0};
int ans = -2;
while (1) {
if (que.empty())
break;
T u = que.front();
que.pop();
if (visit[u.x][u.y] == 1)
continue;
visit[u.x][u.y] = 1;
if (u.x == w - 1 && u.y == h - 1) {
ans = u.ans;
break;
}
int x = u.x;
int y = u.y;
if (x < w - 1 && t[x][y] != 1)
que.push(T(x + 1, y, u.ans + 1));
if (y < h - 1 && z[x][y] != 1)
que.push(T(x, y + 1, u.ans + 1));
if (x > 0 && t[x - 1][y] != 1)
que.push(T(x - 1, y, u.ans + 1));
if (y > 0 && z[x][y - 1] != 1)
que.push(T(x, y - 1, u.ans + 1));
}
printf("%d\n", ans + 1);
goto A;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;
class T {
public:
int x, y, ans;
T(int x, int y, int ans) : x(x), y(y), ans(ans) {}
};
int main() {
int w, h;
A:
cin >> w >> h;
if (w == 0)
return 0;
int t[31][31] = {0};
int z[31][31] = {0};
rep(i, h) {
rep(j, w - 1) { cin >> t[j][i]; }
if (i != h - 1)
rep(j, w) { cin >> z[j][i]; }
}
/*
rep(i,h){
rep(j,w)printf("%d ",t[i][j]);puts("");
rep(j,w)printf("%d ",y[i][j]);puts("");
puts("");
}*/
queue<T> que;
que.push(T(0, 0, 0));
int visit[31][31] = {0};
int ans = -1;
while (1) {
if (que.empty())
break;
T u = que.front();
que.pop();
if (visit[u.x][u.y] == 1)
continue;
visit[u.x][u.y] = 1;
if (u.x == w - 1 && u.y == h - 1) {
ans = u.ans;
break;
}
int x = u.x;
int y = u.y;
if (x < w - 1 && t[x][y] != 1)
que.push(T(x + 1, y, u.ans + 1));
if (y < h - 1 && z[x][y] != 1)
que.push(T(x, y + 1, u.ans + 1));
if (x > 0 && t[x - 1][y] != 1)
que.push(T(x - 1, y, u.ans + 1));
if (y > 0 && z[x][y - 1] != 1)
que.push(T(x, y - 1, u.ans + 1));
}
printf("%d\n", ans + 1);
goto A;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 521
|
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "[" << p.first << ", " << p.second << "]";
return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
bool b = 1;
for (auto s : t)
os << (exchange(b, 0) ||
strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
? ""
: " ")
<< s;
return os << endl;
}
using Node = tuple<int, int, int, double>; // L,R,W,X
vector<Node> node;
vvi edge;
bool isStable(int p) {
int L, R, W;
double X;
tie(L, R, W, X) = node[p];
X *= W;
bool ok = true;
for (auto child : edge[p]) {
int l, r, w;
double x;
ok &= isStable(child);
tie(l, r, w, x) = node[child];
W += w;
X += w * x;
}
X /= W;
node[p] = Node(L, R, W, X);
return ok and L < X and X < R;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int w, h;
while (cin >> w >> h, w) {
node.clear();
edge.clear();
vector<string> input(h);
vvi field(h, vi(w, -1));
rep(i, h) cin >> input[i];
rep(i, h) rep(j, w) if (input[i][j] != '.') {
int L = 100, R = 0, W = 0;
double X = 0;
vi N(60, 0);
char tgt = input[i][j];
queue<pii> q;
q.emplace(i, j);
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
if (input[y][x] == '.')
continue;
W++;
N[x]++;
input[y][x] = '.';
field[y][x] = node.size();
int v[] = {1, 0, -1, 0};
rep(k, 4) {
int ny = y + v[k], nx = x + v[k ^ 1];
if (ny < 0 or nx < 0 or ny >= h or nx >= w or input[ny][nx] != tgt)
continue;
q.emplace(ny, nx);
}
}
rep(i, 60) { X += (i + 0.5) * N[i]; }
node.emplace_back(L, R, W, X / W);
}
int n = node.size(), p = -1;
edge.resize(n);
range(i, 1, h) rep(j, w) {
if (field[i][j] != -1 and field[i - 1][j] != -1 and
field[i - 1][j] != field[i][j]) {
edge[field[i][j]].push_back(field[i - 1][j]);
get<0>(node[field[i - 1][j]]) = min(get<0>(node[field[i - 1][j]]), j);
get<1>(node[field[i - 1][j]]) =
max(get<1>(node[field[i - 1][j]]), j + 1);
}
if (i == h - 1 and field[i][j] != -1) {
p = field[i][j];
get<0>(node[p]) = min(get<0>(node[p]), j);
get<1>(node[p]) = max(get<1>(node[p]), j);
}
}
for (auto &e : edge) {
sort(all(e));
e.erase(unique(all(e)), e.end());
}
cout << (isStable(p) ? "STABLE" : "UNSTABLE") << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
os << "[" << p.first << ", " << p.second << "]";
return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
bool b = 1;
for (auto s : t)
os << (exchange(b, 0) ||
strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
? ""
: " ")
<< s;
return os << endl;
}
using Node = tuple<int, int, int, double>; // L,R,W,X
vector<Node> node;
vvi edge;
bool isStable(int p) {
int L, R, W;
double X;
tie(L, R, W, X) = node[p];
X *= W;
bool ok = true;
for (auto child : edge[p]) {
int l, r, w;
double x;
ok &= isStable(child);
tie(l, r, w, x) = node[child];
W += w;
X += w * x;
}
X /= W;
node[p] = Node(L, R, W, X);
return ok and L < X and X < R;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int w, h;
while (cin >> w >> h, w) {
node.clear();
edge.clear();
vector<string> input(h);
vvi field(h, vi(w, -1));
rep(i, h) cin >> input[i];
rep(i, h) rep(j, w) if (input[i][j] != '.') {
int L = 100, R = 0, W = 0;
double X = 0;
vi N(60, 0);
char tgt = input[i][j];
queue<pii> q;
q.emplace(i, j);
while (!q.empty()) {
int y, x;
tie(y, x) = q.front();
q.pop();
if (input[y][x] == '.')
continue;
W++;
N[x]++;
input[y][x] = '.';
field[y][x] = node.size();
int v[] = {1, 0, -1, 0};
rep(k, 4) {
int ny = y + v[k], nx = x + v[k ^ 1];
if (ny < 0 or nx < 0 or ny >= h or nx >= w or input[ny][nx] != tgt)
continue;
q.emplace(ny, nx);
}
}
rep(i, 60) { X += (i + 0.5) * N[i]; }
node.emplace_back(L, R, W, X / W);
}
int n = node.size(), p = -1;
edge.resize(n);
range(i, 1, h) rep(j, w) {
if (field[i][j] != -1 and field[i - 1][j] != -1 and
field[i - 1][j] != field[i][j]) {
edge[field[i][j]].push_back(field[i - 1][j]);
get<0>(node[field[i - 1][j]]) = min(get<0>(node[field[i - 1][j]]), j);
get<1>(node[field[i - 1][j]]) =
max(get<1>(node[field[i - 1][j]]), j + 1);
}
if (i == h - 1 and field[i][j] != -1) {
p = field[i][j];
get<0>(node[p]) = min(get<0>(node[p]), j);
get<1>(node[p]) = max(get<1>(node[p]), j + 1);
}
}
for (auto &e : edge) {
sort(all(e));
e.erase(unique(all(e)), e.end());
}
cout << (isStable(p) ? "STABLE" : "UNSTABLE") << endl;
}
return 0;
}
|
[["+", 0, 11, 12, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 1,076
|
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
char str[60][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int S[60][11];
int e[200][200];
int L[200];
int R[200];
int Z[200];
int p;
bool ret;
struct wolf {
int v[10];
wolf() {
for (int i = 0; i < 10; i++)
v[i] = 0;
}
};
wolf dat[200];
wolf dfs(int a, int b) {
// printf("%d\n",a);
wolf W = dat[a];
for (int i = 0; i < p; i++)
if (e[a][i] && i != b) {
wolf V = dfs(i, a);
for (int j = 0; j < 10; j++)
W.v[j] += V.v[j];
}
double ave = 0;
int div = 0;
for (int i = 0; i < 10; i++) {
ave += (0.5 + i) * W.v[i];
div += W.v[i];
}
ave /= div;
// printf("%f %d %d\n",ave,L[a],R[a]);
if (ave < 0.00000001 + L[a] || ave > 0.9999999 + R[a])
ret = false;
return W;
}
int main() {
int a, b;
while (scanf("%d%d", &b, &a), a) {
ret = true;
for (int i = 0; i < a; i++) {
scanf("%s", str[a - 1 - i]);
for (int j = 0; j < b; j++)
S[i][j] = 0;
}
p = 1;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (str[i][j] != '.' && S[i][j] == 0) {
queue<pair<int, int>> Q;
Q.push(make_pair(i, j));
S[i][j] = p;
while (Q.size()) {
int row = Q.front().first;
int col = Q.front().second;
Q.pop();
for (int k = 0; k < 4; k++)
if (0 <= row + dx[k] && row + dx[k] < a && 0 <= col + dy[k] &&
col + dy[k] < b &&
str[row][col] == str[row + dx[k]][col + dy[k]] &&
!S[row + dx[k]][col + dy[k]]) {
S[row + dx[k]][col + dy[k]] = p;
Q.push(make_pair(row + dx[k], col + dy[k]));
}
}
p++;
}
}
}
for (int i = 0; i < p; i++)
for (int j = 0; j < p; j++)
e[i][j] = 0;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
for (int k = 0; k < 4; k++)
if (0 <= i + dx[k] && i + dx[k] < a && 0 <= j + dy[k] &&
j + dy[k] < b && S[i][j] && S[i + dx[k]][j + dy[k]] &&
S[i][j] != S[i + dx[k]][j + dy[k]]) {
e[S[i][j]][S[i + dx[k]][j + dy[k]]] = 1;
}
for (int i = 0; i < p; i++) {
L[i] = 100;
R[i] = -1;
Z[i] = -1;
dat[i] = wolf();
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (!S[i][j])
continue;
if (i == 0 || (S[i - 1][j] && S[i - 1][j] != S[i][j])) {
Z[S[i][j]] = i;
L[S[i][j]] = min(L[S[i][j]], j);
R[S[i][j]] = max(R[S[i][j]], j);
}
dat[S[i][j]].v[j]++;
}
}
for (int i = 0; i < b; i++)
if (S[0][i]) {
dfs(S[0][i], -1);
break;
}
if (ret)
printf("STABLE\n");
else
printf("UNSTABLE\n");
}
}
|
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
char str[60][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int S[60][11];
int e[200][200];
int L[200];
int R[200];
int Z[200];
int p;
bool ret;
struct wolf {
int v[10];
wolf() {
for (int i = 0; i < 10; i++)
v[i] = 0;
}
};
wolf dat[200];
wolf dfs(int a, int b) {
// printf("%d\n",a);
wolf W = dat[a];
for (int i = 0; i < p; i++)
if (e[a][i] && i != b) {
wolf V = dfs(i, a);
for (int j = 0; j < 10; j++)
W.v[j] += V.v[j];
}
double ave = 0;
int div = 0;
for (int i = 0; i < 10; i++) {
ave += (0.5 + i) * W.v[i];
div += W.v[i];
}
ave /= div;
// printf("%f %d %d\n",ave,L[a],R[a]);
if (ave < 0.00000001 + L[a] || ave > 0.9999999 + R[a])
ret = false;
return W;
}
int main() {
int a, b;
while (scanf("%d%d", &b, &a), a) {
ret = true;
for (int i = 0; i < a; i++) {
scanf("%s", str[a - 1 - i]);
for (int j = 0; j < b; j++)
S[i][j] = 0;
}
p = 1;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (str[i][j] != '.' && S[i][j] == 0) {
queue<pair<int, int>> Q;
Q.push(make_pair(i, j));
S[i][j] = p;
while (Q.size()) {
int row = Q.front().first;
int col = Q.front().second;
Q.pop();
for (int k = 0; k < 4; k++)
if (0 <= row + dx[k] && row + dx[k] < a && 0 <= col + dy[k] &&
col + dy[k] < b &&
str[row][col] == str[row + dx[k]][col + dy[k]] &&
!S[row + dx[k]][col + dy[k]]) {
S[row + dx[k]][col + dy[k]] = p;
Q.push(make_pair(row + dx[k], col + dy[k]));
}
}
p++;
}
}
}
for (int i = 0; i < p; i++)
for (int j = 0; j < p; j++)
e[i][j] = 0;
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++)
for (int k = 0; k < 1; k++)
if (0 <= i + dx[k] && i + dx[k] < a && 0 <= j + dy[k] &&
j + dy[k] < b && S[i][j] && S[i + dx[k]][j + dy[k]] &&
S[i][j] != S[i + dx[k]][j + dy[k]]) {
e[S[i][j]][S[i + dx[k]][j + dy[k]]] = 1;
}
for (int i = 0; i < p; i++) {
L[i] = 100;
R[i] = -1;
Z[i] = -1;
dat[i] = wolf();
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (!S[i][j])
continue;
if (i == 0 || (S[i - 1][j] && S[i - 1][j] != S[i][j])) {
Z[S[i][j]] = i;
L[S[i][j]] = min(L[S[i][j]], j);
R[S[i][j]] = max(R[S[i][j]], j);
}
dat[S[i][j]].v[j]++;
}
}
for (int i = 0; i < b; i++)
if (S[0][i]) {
dfs(S[0][i], -1);
break;
}
if (ret)
printf("STABLE\n");
else
printf("UNSTABLE\n");
}
}
|
[["-", 0, 7, 8, 7, 8, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 7, 8, 7, 15, 16, 12, 13]]
| 1
| 1,150
|
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
#define all(c) (c).begin(), (c).end()
template <class ForwardIt>
pair<ForwardIt, ForwardIt> sieve(ForwardIt begin, ForwardIt end) {
return make_pair(
begin,
(*begin) * (*begin) <= *end
? sieve(begin + 1, remove_if(begin + 1, end,
[=](int x) { return x % *begin == 0; }))
.second
: end);
}
int main() {
constexpr size_t N = 123456 * 2;
array<int, N> p;
iota(all(p), 2);
auto itr = sieve(all(p));
int n;
while (cin >> n, n)
cout << upper_bound(itr.first, itr.second, n + n) -
upper_bound(itr.first, itr.second, n)
<< '\n';
return 0;
}
|
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
#define all(c) (c).begin(), (c).end()
template <class ForwardIt>
pair<ForwardIt, ForwardIt> sieve(ForwardIt begin, ForwardIt end) {
return make_pair(
begin,
(*begin) * (*begin) <= *(end - 1)
? sieve(begin + 1, remove_if(begin + 1, end,
[=](int x) { return x % *begin == 0; }))
.second
: end);
}
int main() {
constexpr size_t N = 123456 * 2;
array<int, N> p;
iota(all(p), 2);
auto itr = sieve(all(p));
int n;
while (cin >> n, n)
cout << upper_bound(itr.first, itr.second, n + n) -
upper_bound(itr.first, itr.second, n)
<< '\n';
return 0;
}
|
[["+", 0, 41, 15, 16, 12, 66, 28, 23, 0, 24], ["+", 15, 16, 12, 66, 28, 23, 0, 16, 17, 33], ["+", 15, 16, 12, 66, 28, 23, 0, 16, 12, 13], ["+", 0, 41, 15, 16, 12, 66, 28, 23, 0, 25]]
| 1
| 187
|
#include <bits/stdc++.h>
using namespace std;
#define for_(i, a, b) for (int i = (a); i < (b); ++i)
int isp[300000], cnt[300000];
int main() {
memset(cnt, 0, sizeof(cnt));
memset(isp, 0, sizeof(isp));
for_(i, 2, 300000) {
if (isp[i] == 0) {
++cnt[i];
for (int j = 2 * i; j < 300000; j += i)
isp[j] = 1;
}
cnt[i] += cnt[i - 1];
}
int n;
while (cin >> n) {
cout << cnt[2 * n] - cnt[n] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define for_(i, a, b) for (int i = (a); i < (b); ++i)
int isp[300000], cnt[300000];
int main() {
memset(cnt, 0, sizeof(cnt));
memset(isp, 0, sizeof(isp));
for_(i, 2, 300000) {
if (isp[i] == 0) {
++cnt[i];
for (int j = 2 * i; j < 300000; j += i)
isp[j] = 1;
}
cnt[i] += cnt[i - 1];
}
int n;
while (cin >> n, n) {
cout << cnt[2 * n] - cnt[n] << endl;
}
}
|
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
| 1
| 148
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.