problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost \
ios::sync_with_stdio(false); \
cin.tie(nullptr)
#include "string"
const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};
int32_t main() {
boost;
int n, m, k;
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
char c[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cin >> c[i][j];
}
int dp[n + 1][m + 1];
memset(dp, 1, sizeof dp);
dp[x1][y1] = 0;
queue<array<int, 2>> q;
q.push({x1, y1});
while (!q.empty()) {
array<int, 2> p = q.front();
q.pop();
if (p[0] == x2 && p[1] == y2)
break;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int x = p[0] + xx[i] * j;
int y = p[1] + yy[i] * j;
if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
break;
if (dp[x][y] != dp[0][0])
continue;
if (dp[x][y] > dp[p[0]][p[1]] + 1) {
dp[x][y] = dp[p[0]][p[1]] + 1;
q.push({x, y});
}
}
}
}
if (dp[x2][y2] == dp[0][0])
cout << -1 << endl;
else
cout << dp[x2][y2] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost \
ios::sync_with_stdio(false); \
cin.tie(nullptr)
#include "string"
const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};
int32_t main() {
boost;
int n, m, k;
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
char c[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cin >> c[i][j];
}
int dp[n + 1][m + 1];
memset(dp, 1, sizeof dp);
dp[x1][y1] = 0;
queue<array<int, 2>> q;
q.push({x1, y1});
while (!q.empty()) {
array<int, 2> p = q.front();
q.pop();
if (p[0] == x2 && p[1] == y2)
break;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int x = p[0] + xx[i] * j;
int y = p[1] + yy[i] * j;
if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
break;
if (dp[x][y] < dp[p[0]][p[1]] + 1)
break;
if (dp[x][y] > dp[p[0]][p[1]] + 1) {
dp[x][y] = dp[p[0]][p[1]] + 1;
q.push({x, y});
}
}
}
}
if (dp[x2][y2] == dp[0][0])
cout << -1 << endl;
else
cout << dp[x2][y2] << endl;
}
| replace | 42 | 44 | 42 | 44 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0);
const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f;
int tc;
int h, w, k;
string grid[M];
int dir[] = {-1, 1, 0, 0};
int dic[] = {0, 0, -1, 1};
int dis[M], directions[M];
// vector<vector<ll> > dis, directions;
bool valid(int r, int c) {
return r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '@';
}
int main() {
int x1, x2, y1, y2;
scanf("%d %d %d ", &h, &w, &k);
scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
for (int i = 0; i < h; i++) {
string s;
getline(cin, s);
grid[i] = s;
}
x1--;
y1--;
x2--;
y2--;
memset(dis, OO, sizeof dis);
dis[x1 * w + y1] = 0;
// directions[x1 * w + y1] = 15;
queue<pair<ll, ll>> q;
q.push({x1, y1});
while (!q.empty()) {
ll ur = q.front().first, uc = q.front().second;
// cout<<ur<<" "<<uc<<endl;
q.pop();
if (ur == x2 && uc == y2)
break;
int ncost = dis[ur * w + uc] + 1;
// cout<<ur<<" "<<uc<<" "<<dis[ur*w + uc]<<endl;
for (int i = 0; i < 4; i++) {
int steps = 1, vr, vc;
while (steps <= k) {
vr = ur + (dir[i] * steps);
vc = uc + (dic[i] * steps);
int cell = vr * w + vc;
if (valid(vr, vc)) {
if (dis[cell] > ncost) {
dis[cell] = ncost;
q.push({vr, vc});
} else if (dis[cell] == ncost) {
steps++;
continue;
}
} else
break;
steps++;
}
}
}
printf("%d", dis[x2 * w + y2] == OO ? -1 : dis[x2 * w + y2]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0);
const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f;
int tc;
int h, w, k;
string grid[M];
int dir[] = {-1, 1, 0, 0};
int dic[] = {0, 0, -1, 1};
int dis[M], directions[M];
// vector<vector<ll> > dis, directions;
bool valid(int r, int c) {
return r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '@';
}
int main() {
int x1, x2, y1, y2;
scanf("%d %d %d ", &h, &w, &k);
scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
for (int i = 0; i < h; i++) {
string s;
getline(cin, s);
grid[i] = s;
}
x1--;
y1--;
x2--;
y2--;
memset(dis, OO, sizeof dis);
dis[x1 * w + y1] = 0;
// directions[x1 * w + y1] = 15;
queue<pair<ll, ll>> q;
q.push({x1, y1});
while (!q.empty()) {
ll ur = q.front().first, uc = q.front().second;
// cout<<ur<<" "<<uc<<endl;
q.pop();
if (ur == x2 && uc == y2)
break;
int ncost = dis[ur * w + uc] + 1;
// cout<<ur<<" "<<uc<<" "<<dis[ur*w + uc]<<endl;
for (int i = 0; i < 4; i++) {
int steps = 1, vr, vc;
while (steps <= k) {
vr = ur + (dir[i] * steps);
vc = uc + (dic[i] * steps);
int cell = vr * w + vc;
if (valid(vr, vc)) {
if (dis[cell] > ncost) {
dis[cell] = ncost;
q.push({vr, vc});
} else if (dis[cell] == ncost) {
steps++;
continue;
} else
break;
} else
break;
steps++;
}
}
}
printf("%d", dis[x2 * w + y2] == OO ? -1 : dis[x2 * w + y2]);
return 0;
}
| replace | 59 | 60 | 59 | 61 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int
#define _I(i, j) (i * w + j)
int main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
char c[1000000];
rep(i, h) rep(j, w) cin >> c[_I(i, j)];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool mita[1000000] = {0};
queue<int> q, nq;
q.emplace(_I(x1, y1));
mita[_I(x1, y1)] = true;
unordered_map<int, bool> nmita;
int cnt = 0;
while (!q.empty()) {
cnt++;
while (!q.empty()) {
int tx = q.front() / w, ty = q.front() % w;
q.pop();
rep(r, 4) {
for (int s = 1; s <= k; s++) {
int x = tx + dx[r] * s, y = ty + dy[r] * s;
if (x == x2 && y == y2) {
cout << cnt << endl;
return 0;
}
if (x >= 0 && x < h && y >= 0 && y < w && c[_I(x, y)] != '@' &&
!mita[_I(x, y)]) {
nmita[_I(x, y)] = true;
nq.emplace(_I(x, y));
} else
break;
}
}
}
repit(it, nmita) { mita[it->first] = true; }
nmita.clear();
auto tmp = q;
q = nq;
nq = tmp;
}
cout << (-1) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int
#define _I(i, j) (i * w + j)
int main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
char c[1000000];
rep(i, h) rep(j, w) cin >> c[_I(i, j)];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool mita[1000000] = {0};
queue<int> q, nq;
q.emplace(_I(x1, y1));
mita[_I(x1, y1)] = true;
unordered_map<int, bool> nmita;
int cnt = 0;
while (!q.empty()) {
cnt++;
while (!q.empty()) {
int tx = q.front() / w, ty = q.front() % w;
q.pop();
rep(r, 4) {
for (int s = 1; s <= k; s++) {
int x = tx + dx[r] * s, y = ty + dy[r] * s;
if (x == x2 && y == y2) {
cout << cnt << endl;
return 0;
}
if (x >= 0 && x < h && y >= 0 && y < w && c[_I(x, y)] != '@' &&
!mita[_I(x, y)]) {
if (!nmita[_I(x, y)]) {
nmita[_I(x, y)] = true;
nq.emplace(_I(x, y));
}
} else
break;
}
}
}
repit(it, nmita) { mita[it->first] = true; }
nmita.clear();
auto tmp = q;
q = nq;
nq = tmp;
}
cout << (-1) << endl;
return 0;
}
| replace | 47 | 49 | 47 | 51 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
///...................................*****.................................................///
/// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) ///
/// Department of Computer Science /// & Engineering ///
/// Comilla University , Bangladesh. ///
///...................................*****.................................................///
/*....................................Values................................................*/
#define p5 100007
#define p6 1000007
#define PI acos(-1)
#define M 1000000007
#define inf 1LL << 62
#define white 0
#define gray 1
#define black 2
/*....................................Functions.............................................*/
#define sqr(x) x *x
#define sc scanf
#define pf printf
#define pfn printf("\n")
#define scin(x) sc("%d", &(x))
#define scin2(xx, zz) scanf("%d %d", &xx, &zz)
#define scln(x) sc("%lld", &(x))
#define scln2(xx, zz) scanf("%lld %lld", &xx, &zz)
#define min3(a, b, c) min(a, b < c ? b : c)
#define max3(a, b, c) max(a, b > c ? b : c)
#define all(v) v.begin(), v.end()
#define ok cout << "ok" << endl
#define mem(x, y) memset(x, y, sizeof(x))
#define clr(a) a.clear()
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
/*...................................Data_Types............................................*/
#define lli long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define veci vector<int>
#define vecl vector<long long int>
#define vecp vector<pair<int, int>>
#define mapstrint map<string, int>
#define mapstrstr map<string, string>
#define mapint map<int, int>
#define uset unordered_set
#define umap unordered_map
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
/*.....................................Loops...............................................*/
#define rep(i, a, b) for (i = a; i < b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)
#define repx(i, a, b, x) for (i = a; i < b; i += x)
#define test(t) \
int t; \
scin(t); \
while (t--)
#define doshomik(x) fixed << setprecision(x)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
// int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
///------------------------------- Mudular
///functions----------------------------------------
/*
inline lli power(lli x, lli y){ lli temp; if( y == 0) return 1; temp = power(x,
y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } inline lli
add(lli a, lli b) {a += b; return a >= M ? a - M : a;} inline lli sub(lli a, lli
b) {a -= b; return a < 0 ? a + M : a;} inline lli mul(lli a, lli b) {return (a
* b) % M;} lli gcd(lli x,lli y)
{
if(x==0) return y;
return gcd(y%x,x);
}
lli bigmod(lli n, lli k )
{
lli ans=1;
while(k)
{
if(k&1)
ans=(ans*n)%M;
k=k>>1;
n=(n*n)%M;
}
return ans;
}
*/
///----------------------------------Graph
///moves----------------------------------------
int dx4[5] = {1, -1, 0, 0};
int dy4[5] = {0, 0, 1, -1};
int dx8[9] = {0, 0, 1, -1, -1, 1, -1, 1};
int dy8[9] = {-1, 1, 0, 0, 1, 1, -1, -1};
int knightx[9] = {-2, -2, -1, -1, 1, 1, 2, 2};
int knighty[9] = {-1, 1, -2, 2, -2, 2, -1, 1};
bool valid(int r, int c, int x, int y) {
if (x >= 1 && x <= r && y >= 1 && y <= c)
return 1;
return 0;
}
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << "[ " << *it << " = " << a << " ] ";
err(++it, args...);
}
///...............................Code Starts
///Here........................................
vector<vector<lli>> dp;
vector<vector<char>> v;
lli h, w, k;
void initialize_dp(vector<vector<lli>> &dp) {
lli i, j;
rep(i, 1, h + 1) rep(j, 1, w + 1) dp[i][j] = 1LL << 32;
}
lli bfs(lli x1, lli y1, lli x2, lli y2) {
lli i, j, x, y, a, b, level = 0;
queue<pair<lli, lli>> q;
q.push({x1, y1});
dp[x1][y1] = level;
while (!q.empty()) {
x = q.front().ff;
y = q.front().ss;
// vis[x][y]=1;
q.pop();
rep(i, 0, 4) {
rep(j, 1, k + 1) {
a = x + (j * dx4[i]);
b = y + (j * dy4[i]);
if (valid(h, w, a, b)) {
if (v[a][b] != '@' and dp[a][b] > dp[x][y]) {
q.push({a, b}), dp[a][b] = min(dp[a][b], 1 + dp[x][y]);
} else
break;
} else
break;
}
}
}
if (dp[x2][y2] == (1LL << 32))
dp[x2][y2] = -1;
return dp[x2][y2];
}
int main() {
IOS;
lli x1, x2, y1, y2, i, j;
cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
v.resize(h + 3);
dp.resize(h + 3);
rep(i, 1, h + 1) {
v[i].resize(w + 3);
dp[i].resize(w + 3);
rep(j, 1, w + 1) { cin >> v[i][j]; }
}
initialize_dp(dp);
dp[x1][y1] = 0;
lli mini = bfs(x1, y1, x2, y2);
cout << mini << endl;
return 0;
}
| #include <bits/stdc++.h>
///...................................*****.................................................///
/// Author : Raihan Khan Raka ( raihankhanraka@gmail.com ) ///
/// Department of Computer Science /// & Engineering ///
/// Comilla University , Bangladesh. ///
///...................................*****.................................................///
/*....................................Values................................................*/
#define p5 100007
#define p6 1000007
#define PI acos(-1)
#define M 1000000007
#define inf 1LL << 62
#define white 0
#define gray 1
#define black 2
/*....................................Functions.............................................*/
#define sqr(x) x *x
#define sc scanf
#define pf printf
#define pfn printf("\n")
#define scin(x) sc("%d", &(x))
#define scin2(xx, zz) scanf("%d %d", &xx, &zz)
#define scln(x) sc("%lld", &(x))
#define scln2(xx, zz) scanf("%lld %lld", &xx, &zz)
#define min3(a, b, c) min(a, b < c ? b : c)
#define max3(a, b, c) max(a, b > c ? b : c)
#define all(v) v.begin(), v.end()
#define ok cout << "ok" << endl
#define mem(x, y) memset(x, y, sizeof(x))
#define clr(a) a.clear()
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
/*...................................Data_Types............................................*/
#define lli long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define veci vector<int>
#define vecl vector<long long int>
#define vecp vector<pair<int, int>>
#define mapstrint map<string, int>
#define mapstrstr map<string, string>
#define mapint map<int, int>
#define uset unordered_set
#define umap unordered_map
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
/*.....................................Loops...............................................*/
#define rep(i, a, b) for (i = a; i < b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)
#define repx(i, a, b, x) for (i = a; i < b; i += x)
#define test(t) \
int t; \
scin(t); \
while (t--)
#define doshomik(x) fixed << setprecision(x)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
// int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
///------------------------------- Mudular
///functions----------------------------------------
/*
inline lli power(lli x, lli y){ lli temp; if( y == 0) return 1; temp = power(x,
y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } inline lli
add(lli a, lli b) {a += b; return a >= M ? a - M : a;} inline lli sub(lli a, lli
b) {a -= b; return a < 0 ? a + M : a;} inline lli mul(lli a, lli b) {return (a
* b) % M;} lli gcd(lli x,lli y)
{
if(x==0) return y;
return gcd(y%x,x);
}
lli bigmod(lli n, lli k )
{
lli ans=1;
while(k)
{
if(k&1)
ans=(ans*n)%M;
k=k>>1;
n=(n*n)%M;
}
return ans;
}
*/
///----------------------------------Graph
///moves----------------------------------------
int dx4[5] = {1, -1, 0, 0};
int dy4[5] = {0, 0, 1, -1};
int dx8[9] = {0, 0, 1, -1, -1, 1, -1, 1};
int dy8[9] = {-1, 1, 0, 0, 1, 1, -1, -1};
int knightx[9] = {-2, -2, -1, -1, 1, 1, 2, 2};
int knighty[9] = {-1, 1, -2, 2, -2, 2, -1, 1};
bool valid(int r, int c, int x, int y) {
if (x >= 1 && x <= r && y >= 1 && y <= c)
return 1;
return 0;
}
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << "[ " << *it << " = " << a << " ] ";
err(++it, args...);
}
///...............................Code Starts
///Here........................................
vector<vector<lli>> dp;
vector<vector<char>> v;
lli h, w, k;
void initialize_dp(vector<vector<lli>> &dp) {
lli i, j;
rep(i, 1, h + 1) rep(j, 1, w + 1) dp[i][j] = 1LL << 32;
}
lli bfs(lli x1, lli y1, lli x2, lli y2) {
lli i, j, x, y, a, b, level = 0;
queue<pair<lli, lli>> q;
q.push({x1, y1});
dp[x1][y1] = level;
while (!q.empty()) {
x = q.front().ff;
y = q.front().ss;
// vis[x][y]=1;
q.pop();
rep(i, 0, 4) {
rep(j, 1, k + 1) {
a = x + (j * dx4[i]);
b = y + (j * dy4[i]);
if (valid(h, w, a, b)) {
if (v[a][b] != '@' and dp[a][b] > dp[x][y]) {
if (dp[a][b] > (dp[x][y] + 1))
dp[a][b] = dp[x][y] + 1, q.push({a, b});
} else
break;
} else
break;
}
}
}
if (dp[x2][y2] == (1LL << 32))
dp[x2][y2] = -1;
return dp[x2][y2];
}
int main() {
IOS;
lli x1, x2, y1, y2, i, j;
cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
v.resize(h + 3);
dp.resize(h + 3);
rep(i, 1, h + 1) {
v[i].resize(w + 3);
dp[i].resize(w + 3);
rep(j, 1, w + 1) { cin >> v[i][j]; }
}
initialize_dp(dp);
dp[x1][y1] = 0;
lli mini = bfs(x1, y1, x2, y2);
cout << mini << endl;
return 0;
}
| replace | 156 | 157 | 156 | 158 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
<< TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z) \
if (DEBUG) \
cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
<< TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z \
<< endl;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
lli h, w, k;
cin >> h >> w >> k;
lli sx, sy, ex, ey;
cin >> sy >> sx >> ey >> ex;
sx--, sy--, ex--, ey--;
vector<vector<lli>> d(h, vector<lli>(w));
vector<vector<lli>> dd(h, vector<lli>(w));
REP(i, 0, h) REP(j, 0, w) {
char tmp;
cin >> tmp;
if (tmp == '@')
d[i][j] = 1;
else
d[i][j] = 0;
dd[i][j] = -1;
}
queue<lli> qx, qy;
qx.push(sx);
qy.push(sy);
dd[sy][sx] = 0;
while (qx.size()) {
lli topX = qx.front();
lli topY = qy.front();
LOG3(topX, topY, dd[topY][topX]);
qx.pop(), qy.pop();
lli dx[4] = {-1, 0, 1, 0};
lli dy[4] = {0, 1, 0, -1};
REP(i, 0, 4) {
REP(K, 1, k + 1) {
lli nx = topX + dx[i] * K;
lli ny = topY + dy[i] * K;
LOG2(nx, ny);
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
break;
if (d[ny][nx] == 1)
break;
if (dd[ny][nx] != -1 && dd[ny][nx] < dd[topY][topX] + 1)
break;
qx.push(nx);
qy.push(ny);
dd[ny][nx] = dd[topY][topX] + 1;
}
}
}
LOG2(ey, ex);
if (dd[ey][ex] == -1) {
cout << -1 << endl;
} else {
cout << dd[ey][ex] << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z) \
if (DEBUG) \
cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \
<< TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z) \
if (DEBUG) \
cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \
<< TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z \
<< endl;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int main() {
lli h, w, k;
cin >> h >> w >> k;
lli sx, sy, ex, ey;
cin >> sy >> sx >> ey >> ex;
sx--, sy--, ex--, ey--;
vector<vector<lli>> d(h, vector<lli>(w));
vector<vector<lli>> dd(h, vector<lli>(w));
REP(i, 0, h) REP(j, 0, w) {
char tmp;
cin >> tmp;
if (tmp == '@')
d[i][j] = 1;
else
d[i][j] = 0;
dd[i][j] = -1;
}
queue<lli> qx, qy;
qx.push(sx);
qy.push(sy);
dd[sy][sx] = 0;
while (qx.size()) {
lli topX = qx.front();
lli topY = qy.front();
LOG3(topX, topY, dd[topY][topX]);
qx.pop(), qy.pop();
lli dx[4] = {-1, 0, 1, 0};
lli dy[4] = {0, 1, 0, -1};
REP(i, 0, 4) {
REP(K, 1, k + 1) {
lli nx = topX + dx[i] * K;
lli ny = topY + dy[i] * K;
LOG2(nx, ny);
if (nx < 0 || w <= nx || ny < 0 || h <= ny)
break;
if (d[ny][nx] == 1)
break;
if (dd[ny][nx] != -1 && dd[ny][nx] < dd[topY][topX] + 1)
break;
if (dd[ny][nx] != -1)
continue;
qx.push(nx);
qy.push(ny);
dd[ny][nx] = dd[topY][topX] + 1;
}
}
}
LOG2(ey, ex);
if (dd[ey][ex] == -1) {
cout << -1 << endl;
} else {
cout << dd[ey][ex] << endl;
}
return 0;
} | insert | 89 | 89 | 89 | 91 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
ll h, w, k;
ll X1, Y1, X2, Y2;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w >> k;
cin >> X1 >> Y1 >> X2 >> Y2;
X1--;
Y1--;
X2--;
Y2--;
vector<vector<char>> field(h + 1, vector<char>(w + 1));
vector<vector<ll>> dist(h + 1, vector<ll>(w + 1));
rep(i, h) {
rep(j, w) {
cin >> field[i][j];
dist[i][j] = -1;
}
}
queue<pair<ll, ll>> que;
dist[X1][Y1] = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
que.push({X1, Y1});
while (!que.empty()) {
ll xnow = que.front().first;
ll ynow = que.front().second;
que.pop();
rep(dir, 4) {
rep(i, k) { // 進めるマスは1~k
ll xnx = xnow + dx[dir] * (i + 1);
ll ynx = ynow + dy[dir] * (i + 1);
if (xnx < 0 || xnx >= h)
break;
if (ynx < 0 || ynx >= w)
break;
if (field[xnx][ynx] == '@') {
break;
}
if (dist[xnx][ynx] != -1 && (dist[xnx][ynx] <= dist[xnow][ynow]))
break;
dist[xnx][ynx] = dist[xnow][ynow] + 1;
que.push({xnx, ynx});
}
}
}
cout << dist[X2][Y2] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const ll INF = 1e18;
ll h, w, k;
ll X1, Y1, X2, Y2;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w >> k;
cin >> X1 >> Y1 >> X2 >> Y2;
X1--;
Y1--;
X2--;
Y2--;
vector<vector<char>> field(h + 1, vector<char>(w + 1));
vector<vector<ll>> dist(h + 1, vector<ll>(w + 1));
rep(i, h) {
rep(j, w) {
cin >> field[i][j];
dist[i][j] = -1;
}
}
queue<pair<ll, ll>> que;
dist[X1][Y1] = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
que.push({X1, Y1});
while (!que.empty()) {
ll xnow = que.front().first;
ll ynow = que.front().second;
que.pop();
rep(dir, 4) {
rep(i, k) { // 進めるマスは1~k
ll xnx = xnow + dx[dir] * (i + 1);
ll ynx = ynow + dy[dir] * (i + 1);
if (xnx < 0 || xnx >= h)
break;
if (ynx < 0 || ynx >= w)
break;
if (field[xnx][ynx] == '@') {
break;
}
if (dist[xnx][ynx] != -1 && (dist[xnx][ynx] <= dist[xnow][ynow]))
break;
if (dist[xnx][ynx] != -1)
continue;
dist[xnx][ynx] = dist[xnow][ynow] + 1;
que.push({xnx, ynx});
}
}
}
cout << dist[X2][Y2] << endl;
return 0;
}
| insert | 64 | 64 | 64 | 66 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;
int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;
bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }
bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }
void bfs() {
queue<pair<int, int>> que;
que.push({sx, sy});
dis[sx][sy] = 0;
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x, ny = y;
int nd = dis[x][y] + 1;
for (int j = 1; j <= k; j++) {
nx += dir[i][0];
ny += dir[i][1];
if (outside(nx, ny) or bad(nx, ny, nd))
break;
que.push({nx, ny});
dis[nx][ny] = nd;
}
}
}
}
int main() {
cin >> h >> w >> k >> sx >> sy >> gx >> gy;
--sx, --sy, --gx, --gy;
MP.resize(h, vector<char>(w));
dis.resize(h, vector<int>(w));
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> MP[i][j], dis[i][j] = INF;
bfs();
cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}
| #include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;
int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;
bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }
bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }
void bfs() {
queue<pair<int, int>> que;
que.push({sx, sy});
dis[sx][sy] = 0;
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x, ny = y;
int nd = dis[x][y] + 1;
for (int j = 1; j <= k; j++) {
nx += dir[i][0];
ny += dir[i][1];
if (outside(nx, ny) or bad(nx, ny, nd))
break;
if (dis[nx][ny] > nd) {
que.push({nx, ny});
dis[nx][ny] = nd;
}
}
}
}
}
int main() {
cin >> h >> w >> k >> sx >> sy >> gx >> gy;
--sx, --sy, --gx, --gy;
MP.resize(h, vector<char>(w));
dis.resize(h, vector<int>(w));
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> MP[i][j], dis[i][j] = INF;
bfs();
cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}
| replace | 30 | 32 | 30 | 34 | TLE | |
p02644 | C++ | Time Limit Exceeded | #pragma region header
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define int long long
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define mt make_tuple
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
#define split_pair(f, s, p) \
auto f = p.first; \
auto s = p.second
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using vvvp = vector<vvp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;
const int INF = 1e18;
const int MOD = 1e9 + 7;
int mod(int a) { return (a % MOD + MOD) % MOD; }
int m_add(int a, int b) { return (a + b) % MOD; }
int m_add(int a, int b, int c) { return (a + b + c) % MOD; }
int m_sub(int a, int b) { return (a + MOD - b) % MOD; }
int m_mul(int a, int b) { return a * b % MOD; }
int m_mul(int a, int b, int c) { return a * b % MOD * c % MOD; }
int m_bipow(int x, int y) {
if (y == 0)
return 1;
else if (y == 1)
return x % MOD;
else if (y % 2 == 0) {
int z = m_bipow(x, (int)(y / 2));
return m_mul(z, z);
} else {
int z = m_bipow(x, (int)(y / 2));
return m_mul(z, z, x);
}
}
int m_inv(int x) { return m_bipow(x, MOD - 2); }
int m_div(int a, int b) { return m_mul(a, m_inv(b)); }
template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(rall(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(T a) { return *min_element(all(a)); }
template <class T> auto max_of(T a) { return *max_element(all(a)); }
template <class T> int sum_of(T a) { return accumulate(all(a), 0ll); }
template <class T, class U> auto sum_of(T a, U init) {
return accumulate(all(a), init);
}
template <class T, class U> int count_of(T a, U i) { return count(all(a), i); }
template <class T, class U> int lower_index(T a, U i) {
return lower_bound(all(a), i) - begin(a);
} // use member func for set
template <class T, class U> int upper_index(T a, U i) {
return upper_bound(all(a), i) - begin(a);
}
template <class T, class U> bool binary(T a, U i) {
return binary_search(all(a), i);
}
template <class T, class U> bool exists(T a, U i) {
return find(all(a), i) != end(a);
}
template <class T> int sz(T a) { return a.size(); };
template <class T> void COUT(T x) { cout << x << endl; }
template <class T, class U> void COUT(T x, U y) {
cout << x << ' ' << y << endl;
}
template <class T, class U, class V> void COUT(T x, U y, V z) {
cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(T x) { cout << x << ' '; }
template <class T> void CVEC(T v) {
int c = v.size() - 1;
for (size_t i = 0; i < c; i++)
cout << v[i] << ' ';
if (c > -1)
cout << v[c];
cout << endl;
}
template <class T> bool amin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> vector<pair<T, int>> indexed_vector(vector<T> v) {
int n = sz(v);
vector<pair<T, int>> w(n);
for (int i = 0; i < n; i++)
w[i] = make_pair(v[i], i);
return w;
}
template <class T, class S> vector<pair<T, S>> zip(vector<T> v, vector<S> w) {
int n = min(sz(v), sz(w));
vector<pair<T, S>> x(n);
for (int i = 0; i < n; i++)
x[i] = make_pair(v[i], w[i]);
return x;
}
template <class T, class S>
pair<vector<T>, vector<S>> unzip(vector<pair<T, S>> v) {
int n = sz(v);
auto w = make_pair(vector<T>(n), vector<S>(n));
for (int i = 0; i < n; i++) {
w.first[i] = v[i].first;
w.second[i] = v[i].second;
}
return w;
}
vs split(const string &s, string d) {
vs elms;
size_t offset = 0, d_size = d.size();
while (true) {
size_t next = s.find_first_of(d, offset);
if (next == string::npos) {
elms.push_back(s.substr(offset));
return elms;
}
elms.push_back(s.substr(offset, next - offset));
offset = next + d_size;
}
}
vs split(const string &s, char d) { return split(s, string(1, d)); }
string join(const vs &v, const string d = "") {
string s;
if (!v.empty()) {
s += v[0];
for (size_t i = 1, c = v.size(); i < c; ++i) {
if (!d.empty())
s += d;
s += v[i];
}
}
return s;
}
string join(const vs &v, const char d) { return join(v, string(1, d)); }
string pad_left(string s, int width, char filler = '0') {
int n = sz(s);
if (n > width)
return s.substr(n - width);
return string(width - n, filler) + s;
}
int ceil_div(int x, int y) { return (x - 1) / y + 1; }
#pragma endregion header
// MOD = 1e9 + 7;
vvi grid_bfs(vs &s, char start, int k) {
const int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};
vvi min_cost(s.size(), vi(s[0].size(), -1));
queue<pii> que;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < s[i].size(); j++) {
if (s[i][j] == start) {
que.emplace(i, j);
min_cost[i][j] = 0;
}
}
}
while (!que.empty()) {
auto p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first, nx = p.second;
rep(_, k) {
ny += vy[i];
nx += vx[i];
if (nx < 0 || ny < 0 || nx >= s[0].size() || ny >= s.size())
break;
if (s[ny][nx] == '@')
break;
if (min_cost[ny][nx] != -1)
continue;
min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
que.emplace(ny, nx);
}
}
}
return min_cost;
}
void solve() {
int h, w, k;
int ans = 0;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
s[x1][y1] = 'S';
auto gb = grid_bfs(s, 'S', k);
cout << gb[x2][y2] << endl;
}
// Failed to predict input format
#pragma region main
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
solve();
}
#pragma endregion main
| #pragma region header
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define int long long
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define mp make_pair
#define mt make_tuple
#define rep1(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define rrep1(i, n) for (auto i = n - 1; i > static_cast<decltype(i)>(-1); i--)
#define rep2(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep2(i, a, b) for (auto i = b - 1; i >= a; i--)
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define rrep(...) GET_MACRO(__VA_ARGS__, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : (a))
#define split_pair(f, s, p) \
auto f = p.first; \
auto s = p.second
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using pii = pair<int, int>;
using vp = vector<pii>;
using vvp = vector<vp>;
using vvvp = vector<vvp>;
using mii = map<int, int>;
using vm = vector<mii>;
using vvm = vector<vm>;
using vvvm = vector<vvm>;
const int INF = 1e18;
const int MOD = 1e9 + 7;
int mod(int a) { return (a % MOD + MOD) % MOD; }
int m_add(int a, int b) { return (a + b) % MOD; }
int m_add(int a, int b, int c) { return (a + b + c) % MOD; }
int m_sub(int a, int b) { return (a + MOD - b) % MOD; }
int m_mul(int a, int b) { return a * b % MOD; }
int m_mul(int a, int b, int c) { return a * b % MOD * c % MOD; }
int m_bipow(int x, int y) {
if (y == 0)
return 1;
else if (y == 1)
return x % MOD;
else if (y % 2 == 0) {
int z = m_bipow(x, (int)(y / 2));
return m_mul(z, z);
} else {
int z = m_bipow(x, (int)(y / 2));
return m_mul(z, z, x);
}
}
int m_inv(int x) { return m_bipow(x, MOD - 2); }
int m_div(int a, int b) { return m_mul(a, m_inv(b)); }
template <class T> void SORT(T &a) { stable_sort(all(a)); }
template <class T> void RSORT(T &a) { stable_sort(rall(a)); }
template <class T> void rev(T &a) { reverse(rall(a)); }
template <class T> void uniq(T &a) { a.erase(unique(all(a)), end(a)); }
template <class T> auto min_of(T a) { return *min_element(all(a)); }
template <class T> auto max_of(T a) { return *max_element(all(a)); }
template <class T> int sum_of(T a) { return accumulate(all(a), 0ll); }
template <class T, class U> auto sum_of(T a, U init) {
return accumulate(all(a), init);
}
template <class T, class U> int count_of(T a, U i) { return count(all(a), i); }
template <class T, class U> int lower_index(T a, U i) {
return lower_bound(all(a), i) - begin(a);
} // use member func for set
template <class T, class U> int upper_index(T a, U i) {
return upper_bound(all(a), i) - begin(a);
}
template <class T, class U> bool binary(T a, U i) {
return binary_search(all(a), i);
}
template <class T, class U> bool exists(T a, U i) {
return find(all(a), i) != end(a);
}
template <class T> int sz(T a) { return a.size(); };
template <class T> void COUT(T x) { cout << x << endl; }
template <class T, class U> void COUT(T x, U y) {
cout << x << ' ' << y << endl;
}
template <class T, class U, class V> void COUT(T x, U y, V z) {
cout << x << ' ' << y << ' ' << z << endl;
}
template <class T> void CSP(T x) { cout << x << ' '; }
template <class T> void CVEC(T v) {
int c = v.size() - 1;
for (size_t i = 0; i < c; i++)
cout << v[i] << ' ';
if (c > -1)
cout << v[c];
cout << endl;
}
template <class T> bool amin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> bool amax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> vector<pair<T, int>> indexed_vector(vector<T> v) {
int n = sz(v);
vector<pair<T, int>> w(n);
for (int i = 0; i < n; i++)
w[i] = make_pair(v[i], i);
return w;
}
template <class T, class S> vector<pair<T, S>> zip(vector<T> v, vector<S> w) {
int n = min(sz(v), sz(w));
vector<pair<T, S>> x(n);
for (int i = 0; i < n; i++)
x[i] = make_pair(v[i], w[i]);
return x;
}
template <class T, class S>
pair<vector<T>, vector<S>> unzip(vector<pair<T, S>> v) {
int n = sz(v);
auto w = make_pair(vector<T>(n), vector<S>(n));
for (int i = 0; i < n; i++) {
w.first[i] = v[i].first;
w.second[i] = v[i].second;
}
return w;
}
vs split(const string &s, string d) {
vs elms;
size_t offset = 0, d_size = d.size();
while (true) {
size_t next = s.find_first_of(d, offset);
if (next == string::npos) {
elms.push_back(s.substr(offset));
return elms;
}
elms.push_back(s.substr(offset, next - offset));
offset = next + d_size;
}
}
vs split(const string &s, char d) { return split(s, string(1, d)); }
string join(const vs &v, const string d = "") {
string s;
if (!v.empty()) {
s += v[0];
for (size_t i = 1, c = v.size(); i < c; ++i) {
if (!d.empty())
s += d;
s += v[i];
}
}
return s;
}
string join(const vs &v, const char d) { return join(v, string(1, d)); }
string pad_left(string s, int width, char filler = '0') {
int n = sz(s);
if (n > width)
return s.substr(n - width);
return string(width - n, filler) + s;
}
int ceil_div(int x, int y) { return (x - 1) / y + 1; }
#pragma endregion header
// MOD = 1e9 + 7;
vvi grid_bfs(vs &s, char start, int k) {
const int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};
vvi min_cost(s.size(), vi(s[0].size(), -1));
queue<pii> que;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < s[i].size(); j++) {
if (s[i][j] == start) {
que.emplace(i, j);
min_cost[i][j] = 0;
}
}
}
while (!que.empty()) {
auto p = que.front();
que.pop();
for (int i = 0; i < 4; i++) {
int ny = p.first, nx = p.second;
rep(_, k) {
ny += vy[i];
nx += vx[i];
if (nx < 0 || ny < 0 || nx >= s[0].size() || ny >= s.size())
break;
if (s[ny][nx] == '@')
break;
int n_min = min_cost[ny][nx];
if (n_min != -1) {
if (min_cost[p.first][p.second] < n_min)
continue;
else
break;
}
if (min_cost[ny][nx] != -1)
continue;
min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
que.emplace(ny, nx);
}
}
}
return min_cost;
}
void solve() {
int h, w, k;
int ans = 0;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
s[x1][y1] = 'S';
auto gb = grid_bfs(s, 'S', k);
cout << gb[x2][y2] << endl;
}
// Failed to predict input format
#pragma region main
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
solve();
}
#pragma endregion main
| insert | 213 | 213 | 213 | 220 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD = 1e9 + 7;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
int n, m, K;
bool issafe(int x, int y) { return (x >= 0 && x < n) && (y >= 0 && y < m); }
int main() {
cin >> n >> m >> K;
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
--sx, --sy, --ex, --ey;
vector<string> grid(n);
for (int i = 0; i < n; i++)
cin >> grid[i];
vector<vector<int>> dis(n, vector<int>(m, -1));
dis[sx][sy] = 0;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
queue<pair<int, int>> q;
q.push({sx, sy});
while (!q.empty()) {
auto c = q.front();
q.pop();
auto cx = c.first, cy = c.second;
// cout << cx << " " << cy << " " << dis[cx][cy] << endl;
for (int i = 0; i < 4; i++) {
for (int k = 1; k <= K; k++) {
int nx = cx + (dx[i] * k);
int ny = cy + (dy[i] * k);
if (!issafe(nx, ny) || grid[nx][ny] == '@')
break;
else {
if (dis[nx][ny] == -1) {
dis[nx][ny] = 1 + dis[cx][cy];
q.push({nx, ny});
}
// else if(dis[nx][ny] > dis[cx][cy]+1) {
// dis[nx][ny] = 1 + dis[cx][cy];
// }
}
}
}
}
/* for(int i = 0;i<n;i++)
{
for(int j = 0;j <m;j++)
cout << dis[i][j] << " ";
cout << endl;
}*/
if (dis[ex][ey] == -1)
cout << -1 << endl;
else
cout << dis[ex][ey] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD = 1e9 + 7;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }
int n, m, K;
bool issafe(int x, int y) { return (x >= 0 && x < n) && (y >= 0 && y < m); }
int main() {
cin >> n >> m >> K;
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
--sx, --sy, --ex, --ey;
vector<string> grid(n);
for (int i = 0; i < n; i++)
cin >> grid[i];
vector<vector<int>> dis(n, vector<int>(m, -1));
dis[sx][sy] = 0;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
queue<pair<int, int>> q;
q.push({sx, sy});
while (!q.empty()) {
auto c = q.front();
q.pop();
auto cx = c.first, cy = c.second;
// cout << cx << " " << cy << " " << dis[cx][cy] << endl;
for (int i = 0; i < 4; i++) {
for (int k = 1; k <= K; k++) {
int nx = cx + (dx[i] * k);
int ny = cy + (dy[i] * k);
if (!issafe(nx, ny) || grid[nx][ny] == '@')
break;
else {
if (dis[nx][ny] == -1) {
dis[nx][ny] = 1 + dis[cx][cy];
q.push({nx, ny});
} else if (dis[nx][ny] >= dis[cx][cy] + 1) {
dis[nx][ny] = 1 + dis[cx][cy];
} else
break;
}
}
}
}
/* for(int i = 0;i<n;i++)
{
for(int j = 0;j <m;j++)
cout << dis[i][j] << " ";
cout << endl;
}*/
if (dis[ex][ey] == -1)
cout << -1 << endl;
else
cout << dis[ex][ey] << endl;
return 0;
} | replace | 42 | 46 | 42 | 46 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
ll y;
ll x;
ll depth;
};
ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;
ll bfs() {
ll ans = oo;
while (!q.empty()) {
Z now = q.front();
q.pop();
y = now.y, x = now.x, depth = now.depth;
if (y == yg && x == xg) {
ans = min(ans, depth);
return ans;
}
for (ll i = 0; i < 4; i++) {
for (ll k = 1; k <= K; k++) {
Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
// cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
A[next.y][next.x] == -1)
break;
if (A[next.y][next.x] < next.depth)
break;
A[next.y][next.x] = next.depth;
q.push(next);
// cout<<" d:"<<next.depth<<endl;
}
}
}
return oo;
}
int main() {
cin >> H >> W >> K;
cin >> ys >> xs >> yg >> xg;
A.assign(H + 2, vector<ll>(W + 2, -1));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char a;
cin >> a;
if (a == '@')
continue;
A[i][j] = oo;
}
}
Z start = {ys, xs, 0};
A[ys][xs] = 0;
q.emplace(start);
ll ans = bfs();
if (ans == oo)
ans = -1;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
ll y;
ll x;
ll depth;
};
ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;
ll bfs() {
ll ans = oo;
while (!q.empty()) {
Z now = q.front();
q.pop();
y = now.y, x = now.x, depth = now.depth;
if (y == yg && x == xg) {
ans = min(ans, depth);
return ans;
}
for (ll i = 0; i < 4; i++) {
for (ll k = 1; k <= K; k++) {
Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
// cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
A[next.y][next.x] == -1)
break;
if (A[next.y][next.x] < next.depth)
break;
if (A[next.y][next.x] == next.depth)
continue;
A[next.y][next.x] = next.depth;
q.push(next);
// cout<<" d:"<<next.depth<<endl;
}
}
}
return oo;
}
int main() {
cin >> H >> W >> K;
cin >> ys >> xs >> yg >> xg;
A.assign(H + 2, vector<ll>(W + 2, -1));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char a;
cin >> a;
if (a == '@')
continue;
A[i][j] = oo;
}
}
Z start = {ys, xs, 0};
A[ys][xs] = 0;
q.emplace(start);
ll ans = bfs();
if (ans == oo)
ans = -1;
cout << ans << endl;
}
| insert | 35 | 35 | 35 | 37 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
const int N = 1e5 + 5;
int n, m, k;
int x11, y11, x2, y2;
int dx[4] = {-1, +1, 0, 0};
int dy[4] = {0, 0, +1, -1};
vector<vector<char>> a;
vector<vector<int>> vis, ways, dist;
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({x, y});
vis[x][y] = 1;
dist[x][y] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
for (int len = 1; len <= k; len++) {
int nx = x + dx[dir] * len;
int ny = y + dy[dir] * len;
if (nx < 1 || ny < 1 || nx > n || ny > m)
break;
/*if(vis[nx][ny] && dist[x][y] + 1 > dist[nx][ny])
break;*/
if (a[nx][ny] == '@')
break;
if (vis[nx][ny])
continue;
dist[nx][ny] = dist[x][y] + 1;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
}
}
int32_t main() {
IOS;
cin >> n >> m >> k;
cin >> x11 >> y11 >> x2 >> y2;
a = vector<vector<char>>(n + 1, vector<char>(m + 1));
vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
bfs(x11, y11);
if (!vis[x2][y2])
cout << -1;
else
cout << dist[x2][y2];
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
const int N = 1e5 + 5;
int n, m, k;
int x11, y11, x2, y2;
int dx[4] = {-1, +1, 0, 0};
int dy[4] = {0, 0, +1, -1};
vector<vector<char>> a;
vector<vector<int>> vis, ways, dist;
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({x, y});
vis[x][y] = 1;
dist[x][y] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int dir = 0; dir < 4; dir++) {
for (int len = 1; len <= k; len++) {
int nx = x + dx[dir] * len;
int ny = y + dy[dir] * len;
if (nx < 1 || ny < 1 || nx > n || ny > m)
break;
if (vis[nx][ny] && dist[x][y] + 1 > dist[nx][ny])
break;
if (a[nx][ny] == '@')
break;
if (vis[nx][ny])
continue;
dist[nx][ny] = dist[x][y] + 1;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
}
}
int32_t main() {
IOS;
cin >> n >> m >> k;
cin >> x11 >> y11 >> x2 >> y2;
a = vector<vector<char>>(n + 1, vector<char>(m + 1));
vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
bfs(x11, y11);
if (!vis[x2][y2])
cout << -1;
else
cout << dist[x2][y2];
return 0;
} | replace | 33 | 35 | 33 | 35 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define reps(i, f, n) for (int i = (f); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
const int INF = 1111111111;
const double EPS = 1e-9;
using namespace std;
int h, w, k;
int sx, sy, gx, gy;
vector<string> c;
void init() {
cin >> h >> w >> k;
cin >> sy >> sx >> gy >> gx;
rep(i, h) {
string s;
cin >> s;
c.push_back(s);
}
sx--;
sy--;
gx--;
gy--;
}
void solve() {
queue<piii> que;
que.push(piii(0, pii(sx, sy)));
int ans = -1;
while (!que.empty()) {
piii top = que.front();
que.pop();
int cost = top.first;
int x = top.second.first;
int y = top.second.second;
if (c[y][x] == 'o') {
continue;
}
c[y][x] = 'o';
// cout << "cha " << cost << " " << x << " " << y << endl;
// rep(i, h) cout << c[i] << endl;
if (x == gx && y == gy) {
ans = cost;
break;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
rep(i, 4) {
reps(dist, 1, k + 1) {
int nx = x + dist * dx[i];
int ny = y + dist * dy[i];
if (nx < 0 || nx >= w | ny < 0 || ny >= h) {
break;
}
if (c[ny][nx] == '@') {
break;
}
if (c[ny][nx] != '.') {
continue;
}
c[ny][nx] = 'x';
que.push(piii(cost + 1, pii(nx, ny)));
}
}
}
cout << ans << endl;
}
int main() {
init();
solve();
}
| #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define piii pair<int, pii>
#define reps(i, f, n) for (int i = (f); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
const int INF = 1111111111;
const double EPS = 1e-9;
using namespace std;
int h, w, k;
int sx, sy, gx, gy;
vector<string> c;
void init() {
cin >> h >> w >> k;
cin >> sy >> sx >> gy >> gx;
rep(i, h) {
string s;
cin >> s;
c.push_back(s);
}
sx--;
sy--;
gx--;
gy--;
}
void solve() {
queue<piii> que;
que.push(piii(0, pii(sx, sy)));
int ans = -1;
while (!que.empty()) {
piii top = que.front();
que.pop();
int cost = top.first;
int x = top.second.first;
int y = top.second.second;
if (c[y][x] == 'o') {
continue;
}
c[y][x] = 'o';
// cout << "cha " << cost << " " << x << " " << y << endl;
// rep(i, h) cout << c[i] << endl;
if (x == gx && y == gy) {
ans = cost;
break;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
rep(i, 4) {
reps(dist, 1, k + 1) {
int nx = x + dist * dx[i];
int ny = y + dist * dy[i];
if (nx < 0 || nx >= w | ny < 0 || ny >= h) {
break;
}
if (c[ny][nx] == '@' || c[ny][nx] == 'o') {
break;
}
if (c[ny][nx] != '.') {
continue;
}
c[ny][nx] = 'x';
que.push(piii(cost + 1, pii(nx, ny)));
}
}
}
cout << ans << endl;
}
int main() {
init();
solve();
}
| replace | 79 | 80 | 79 | 80 | TLE | |
p02644 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
using ll = long long int;
#define all(v) (v).begin(), (v).end()
#define repeat(cnt, l) \
for (typename remove_const< \
typename remove_reference<decltype(l)>::type>::type cnt = {}; \
(cnt) < (l); ++(cnt))
#define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt))
#define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt))
#define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt))
const long long MD = 1000000007ll;
const long double PI = 3.1415926535897932384626433832795L;
template <typename T1, typename T2>
inline ostream &operator<<(ostream &o, const pair<T1, T2> p) {
o << '(' << p.first << ':' << p.second << ')';
return o;
}
template <typename T> inline T &chmax(T &to, const T &val) {
return to = max(to, val);
}
template <typename T> inline T &chmin(T &to, const T &val) {
return to = min(to, val);
}
void bye(string s, int code = 0) {
cout << s << endl;
exit(code);
}
mt19937_64 randdev(8901016);
template <typename T, typename Random = decltype(randdev),
typename enable_if<is_integral<T>::value>::type * = nullptr>
inline T rand(T l, T h, Random &rand = randdev) {
return uniform_int_distribution<T>(l, h)(rand);
}
template <typename T, typename Random = decltype(randdev),
typename enable_if<is_floating_point<T>::value>::type * = nullptr>
inline T rand(T l, T h, Random &rand = randdev) {
return uniform_real_distribution<T>(l, h)(rand);
}
template <typename I> struct MyRangeFormat {
I b, e;
MyRangeFormat(I _b, I _e) : b(_b), e(_e) {}
};
template <typename I>
static ostream &operator<<(ostream &o, const MyRangeFormat<I> &f) {
o << "[ ";
iterate(i, f.b, f.e) o << *i << ' ';
return o << ']';
}
template <typename I> struct MyMatrixFormat {
const I &p;
long long n, m;
MyMatrixFormat(const I &_p, long long _n, long long _m)
: p(_p), n(_n), m(_m) {}
};
template <typename I>
static ostream &operator<<(ostream &o, const MyMatrixFormat<I> &f) {
o << '\n';
repeat(i, (f.n)) {
repeat(j, f.m) o << f.p[i][j] << ' ';
o << '\n';
}
return o;
}
struct LOG_t {
~LOG_t() { cout << endl; }
};
#define LOG (LOG_t(), cout << 'L' << __LINE__ << ": ")
#define FMTA(m, w) (MyRangeFormat<decltype(m + 0)>(m, m + w))
#define FMTR(b, e) (MyRangeFormat<decltype(e)>(b, e))
#define FMTV(v) FMTR(v.begin(), v.end())
#define FMTM(m, h, w) (MyMatrixFormat<decltype(m + 0)>(m, h, w))
#if defined(_WIN32) || defined(_WIN64)
#define getc_x _getc_nolock
#define putc_x _putc_nolock
#elif defined(__GNUC__)
#define getc_x getc_unlocked
#define putc_x putc_unlocked
#else
#define getc_x getc
#define putc_x putc
#endif
class MaiScanner {
FILE *fp_;
constexpr bool isvisiblechar(char c) noexcept {
return (0x21 <= (c) && (c) <= 0x7E);
}
public:
inline MaiScanner(FILE *fp) : fp_(fp) {}
template <typename T> void input_integer(T &var) noexcept {
var = 0;
T sign = 1;
int cc = getc_x(fp_);
for (; cc < '0' || '9' < cc; cc = getc_x(fp_))
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_))
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
}
inline int c() noexcept { return getc_x(fp_); }
template <typename T, typename enable_if<is_integral<T>::value,
nullptr_t>::type = nullptr>
inline MaiScanner &operator>>(T &var) noexcept {
input_integer<T>(var);
return *this;
}
inline MaiScanner &operator>>(string &var) {
int cc = getc_x(fp_);
for (; !isvisiblechar(cc); cc = getc_x(fp_))
;
for (; isvisiblechar(cc); cc = getc_x(fp_))
var.push_back(cc);
return *this;
}
template <typename IT> inline void in(IT begin, IT end) {
for (auto it = begin; it != end; ++it)
*this >> *it;
}
};
class MaiPrinter {
FILE *fp_;
public:
inline MaiPrinter(FILE *fp) : fp_(fp) {}
template <typename T> void output_integer(T var) noexcept {
if (var == 0) {
putc_x('0', fp_);
return;
}
if (var < 0)
putc_x('-', fp_), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putc_x(stack[--stack_p], fp_);
}
inline MaiPrinter &operator<<(char c) noexcept {
putc_x(c, fp_);
return *this;
}
template <typename T, typename enable_if<is_integral<T>::value,
nullptr_t>::type = nullptr>
inline MaiPrinter &operator<<(T var) noexcept {
output_integer<T>(var);
return *this;
}
inline MaiPrinter &operator<<(char *str_p) noexcept {
while (*str_p)
putc_x(*(str_p++), fp_);
return *this;
}
inline MaiPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putc_x(*p++, fp_);
return *this;
}
template <typename IT> void join(IT begin, IT end, char sep = ' ') {
for (bool b = 0; begin != end; ++begin, b = 1)
b ? *this << sep << *begin : *this << *begin;
}
};
MaiScanner scanner(stdin);
MaiPrinter printer(stdout);
struct P {
using T = int;
T y, x;
inline explicit P(T _y, T _x) : y(_y), x(_x) {}
inline P() : y(0), x(0) {}
inline bool operator==(P p) const { return y == p.y && x == p.x; }
inline bool operator<(P p) const { return y == p.y ? x < p.x : y < p.y; }
inline P operator+(P p) const { return P(y + p.y, x + p.x); }
inline P operator-(P p) const { return P(y - p.y, x - p.x); }
inline P &operator+=(P p) {
y += p.y;
x += p.x;
return *this;
}
inline P &operator-=(P p) {
y -= p.y;
x -= p.x;
return *this;
}
inline P &operator-=(T m) {
y *= m;
x;
return *this;
}
inline T distM(P p) const { return abs(y - p.y) + abs(x - p.x); }
inline T distC(P p) const { return max(abs(y - p.y), abs(x - p.x)); }
template <typename ITR> ITR nearestM(ITR begin, ITR end) const {
if (begin == end)
return end;
T best = distM(*begin);
ITR besti = begin;
for (ITR it = begin; ++it, it != end;) {
T m = distM(*it);
if (best < m) {
best = m;
besti = it;
}
}
return besti;
}
};
inline ostream &operator<<(ostream &os, P p) {
os << '(' << p.y << ',' << p.x << ')';
return os;
}
const P FourMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)};
const P FiveMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(0, 0)};
const P EightMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1),
P(-1, -1), P(-1, 1), P(1, -1), P(1, 1)};
inline P operator*(P::T m, P p) noexcept { return P(m * p.y, m * p.x); }
template <typename T>
// using T = int;
struct F {
int height, width;
vector<T> data;
F(int h = 1, int w = 1) : height(h), width(w), data(h * w) {}
inline T &operator()(int y, int x) { return data[x + y * width]; }
inline T &operator()(P p) { return data[p.x + p.y * width]; }
inline T operator()(int y, int x) const { return data[x + y * width]; }
inline T operator()(P p) const { return data[p.x + p.y * width]; }
inline bool safe(int y, int x) const {
return 0 <= y && y < height && 0 <= x && x < width;
}
inline bool safe(P p) const {
return 0 <= p.y && p.y < height && 0 <= p.x && p.x < width;
}
inline void fill(T e) { std::fill(all(data), e); }
inline void resize(int h, int w) {
height = h;
width = w;
data.resize(h * w);
}
void print(ostream &os, int setw_arg = 4) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x)
os << setw(setw_arg) << operator()(y, x) << ' ';
os << '\n';
}
}
};
// Edge構造体を定義する無向グラフ
class GraphE {
public:
using W_T = int;
struct Edge {
int u, v;
W_T value;
Edge(int from = 0, int to = 0, W_T value = 0)
: u(from), v(to), value(value) {}
inline int to(int _v) const { return _v == v ? u : v; }
};
size_t n;
vector<vector<int>> vertex_to;
vector<Edge> edges;
explicit GraphE(int n = 1) : n(n), vertex_to(n) {}
inline size_t size() const noexcept { return n; }
void resize(size_t _n) { vertex_to.resize(n = _n); }
void connect(int from, int to, W_T val = 0) {
vertex_to[(size_t)from].push_back((int)edges.size());
vertex_to[(size_t)to].push_back((int)edges.size());
edges.emplace_back(from, to, val);
}
void connect(vector<Edge> &&es) {
edges = move(es);
for (int i = 0; (size_t)i < edges.size(); ++i) {
vertex_to[edges[i].u].push_back(i);
vertex_to[edges[i].v].push_back(i);
}
}
};
//
int H, W, K;
int startx, starty, goalx, goaly;
//
F<int> field;
F<int> dist;
// GraphE graph;
inline int cv(int y, int x) { return y * W + x; }
int main() {
scanner >> H >> W >> K;
scanner >> starty >> startx >> goaly >> goalx;
--startx;
--starty;
--goalx;
--goaly;
field.resize(H, W);
// graph.resize(H*W);
repeat(i, H) {
string str;
scanner >> str;
repeat(j, W) {
char c = str[j];
field(i, j) = c == '@';
}
}
dist.resize(H, W);
dist.fill(1e9);
queue<P> que;
que.emplace(starty, startx);
dist(starty, startx) = 0;
while (!que.empty()) {
auto p = que.front();
que.pop();
auto d = dist(p);
if (p == P(goaly, goalx)) {
cout << d << endl;
exit(0); // memfree by os
}
for (auto v : FourMoving) {
auto p2 = v + p;
repeat(i, K) {
if (!field.safe(p2))
break;
if (field(p2))
break;
if (dist(p2) < d)
break;
if (dist(p2) > d + 1) {
dist(p2) = d + 1;
que.push(p2);
}
p2 += v;
}
}
// repeat(i, H) {
// repeat(j, W) {
// cout << min(dist(i,j), 9);
// }
// cout << endl;
// }
// cout << endl;
}
cout << "-1" << endl;
exit(0);
return 0;
}
| #pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
using ll = long long int;
#define all(v) (v).begin(), (v).end()
#define repeat(cnt, l) \
for (typename remove_const< \
typename remove_reference<decltype(l)>::type>::type cnt = {}; \
(cnt) < (l); ++(cnt))
#define rrepeat(cnt, l) for (auto cnt = (l)-1; 0 <= (cnt); --(cnt))
#define iterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); ++(cnt))
#define diterate(cnt, b, e) for (auto cnt = (b); (cnt) != (e); --(cnt))
const long long MD = 1000000007ll;
const long double PI = 3.1415926535897932384626433832795L;
template <typename T1, typename T2>
inline ostream &operator<<(ostream &o, const pair<T1, T2> p) {
o << '(' << p.first << ':' << p.second << ')';
return o;
}
template <typename T> inline T &chmax(T &to, const T &val) {
return to = max(to, val);
}
template <typename T> inline T &chmin(T &to, const T &val) {
return to = min(to, val);
}
void bye(string s, int code = 0) {
cout << s << endl;
exit(code);
}
mt19937_64 randdev(8901016);
template <typename T, typename Random = decltype(randdev),
typename enable_if<is_integral<T>::value>::type * = nullptr>
inline T rand(T l, T h, Random &rand = randdev) {
return uniform_int_distribution<T>(l, h)(rand);
}
template <typename T, typename Random = decltype(randdev),
typename enable_if<is_floating_point<T>::value>::type * = nullptr>
inline T rand(T l, T h, Random &rand = randdev) {
return uniform_real_distribution<T>(l, h)(rand);
}
template <typename I> struct MyRangeFormat {
I b, e;
MyRangeFormat(I _b, I _e) : b(_b), e(_e) {}
};
template <typename I>
static ostream &operator<<(ostream &o, const MyRangeFormat<I> &f) {
o << "[ ";
iterate(i, f.b, f.e) o << *i << ' ';
return o << ']';
}
template <typename I> struct MyMatrixFormat {
const I &p;
long long n, m;
MyMatrixFormat(const I &_p, long long _n, long long _m)
: p(_p), n(_n), m(_m) {}
};
template <typename I>
static ostream &operator<<(ostream &o, const MyMatrixFormat<I> &f) {
o << '\n';
repeat(i, (f.n)) {
repeat(j, f.m) o << f.p[i][j] << ' ';
o << '\n';
}
return o;
}
struct LOG_t {
~LOG_t() { cout << endl; }
};
#define LOG (LOG_t(), cout << 'L' << __LINE__ << ": ")
#define FMTA(m, w) (MyRangeFormat<decltype(m + 0)>(m, m + w))
#define FMTR(b, e) (MyRangeFormat<decltype(e)>(b, e))
#define FMTV(v) FMTR(v.begin(), v.end())
#define FMTM(m, h, w) (MyMatrixFormat<decltype(m + 0)>(m, h, w))
#if defined(_WIN32) || defined(_WIN64)
#define getc_x _getc_nolock
#define putc_x _putc_nolock
#elif defined(__GNUC__)
#define getc_x getc_unlocked
#define putc_x putc_unlocked
#else
#define getc_x getc
#define putc_x putc
#endif
class MaiScanner {
FILE *fp_;
constexpr bool isvisiblechar(char c) noexcept {
return (0x21 <= (c) && (c) <= 0x7E);
}
public:
inline MaiScanner(FILE *fp) : fp_(fp) {}
template <typename T> void input_integer(T &var) noexcept {
var = 0;
T sign = 1;
int cc = getc_x(fp_);
for (; cc < '0' || '9' < cc; cc = getc_x(fp_))
if (cc == '-')
sign = -1;
for (; '0' <= cc && cc <= '9'; cc = getc_x(fp_))
var = (var << 3) + (var << 1) + cc - '0';
var = var * sign;
}
inline int c() noexcept { return getc_x(fp_); }
template <typename T, typename enable_if<is_integral<T>::value,
nullptr_t>::type = nullptr>
inline MaiScanner &operator>>(T &var) noexcept {
input_integer<T>(var);
return *this;
}
inline MaiScanner &operator>>(string &var) {
int cc = getc_x(fp_);
for (; !isvisiblechar(cc); cc = getc_x(fp_))
;
for (; isvisiblechar(cc); cc = getc_x(fp_))
var.push_back(cc);
return *this;
}
template <typename IT> inline void in(IT begin, IT end) {
for (auto it = begin; it != end; ++it)
*this >> *it;
}
};
class MaiPrinter {
FILE *fp_;
public:
inline MaiPrinter(FILE *fp) : fp_(fp) {}
template <typename T> void output_integer(T var) noexcept {
if (var == 0) {
putc_x('0', fp_);
return;
}
if (var < 0)
putc_x('-', fp_), var = -var;
char stack[32];
int stack_p = 0;
while (var)
stack[stack_p++] = '0' + (var % 10), var /= 10;
while (stack_p)
putc_x(stack[--stack_p], fp_);
}
inline MaiPrinter &operator<<(char c) noexcept {
putc_x(c, fp_);
return *this;
}
template <typename T, typename enable_if<is_integral<T>::value,
nullptr_t>::type = nullptr>
inline MaiPrinter &operator<<(T var) noexcept {
output_integer<T>(var);
return *this;
}
inline MaiPrinter &operator<<(char *str_p) noexcept {
while (*str_p)
putc_x(*(str_p++), fp_);
return *this;
}
inline MaiPrinter &operator<<(const string &str) {
const char *p = str.c_str();
const char *l = p + str.size();
while (p < l)
putc_x(*p++, fp_);
return *this;
}
template <typename IT> void join(IT begin, IT end, char sep = ' ') {
for (bool b = 0; begin != end; ++begin, b = 1)
b ? *this << sep << *begin : *this << *begin;
}
};
MaiScanner scanner(stdin);
MaiPrinter printer(stdout);
struct P {
using T = int;
T y, x;
inline explicit P(T _y, T _x) : y(_y), x(_x) {}
inline P() : y(0), x(0) {}
inline bool operator==(P p) const { return y == p.y && x == p.x; }
inline bool operator<(P p) const { return y == p.y ? x < p.x : y < p.y; }
inline P operator+(P p) const { return P(y + p.y, x + p.x); }
inline P operator-(P p) const { return P(y - p.y, x - p.x); }
inline P &operator+=(P p) {
y += p.y;
x += p.x;
return *this;
}
inline P &operator-=(P p) {
y -= p.y;
x -= p.x;
return *this;
}
inline P &operator-=(T m) {
y *= m;
x;
return *this;
}
inline T distM(P p) const { return abs(y - p.y) + abs(x - p.x); }
inline T distC(P p) const { return max(abs(y - p.y), abs(x - p.x)); }
template <typename ITR> ITR nearestM(ITR begin, ITR end) const {
if (begin == end)
return end;
T best = distM(*begin);
ITR besti = begin;
for (ITR it = begin; ++it, it != end;) {
T m = distM(*it);
if (best < m) {
best = m;
besti = it;
}
}
return besti;
}
};
inline ostream &operator<<(ostream &os, P p) {
os << '(' << p.y << ',' << p.x << ')';
return os;
}
const P FourMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1)};
const P FiveMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1), P(0, 0)};
const P EightMoving[] = {P(-1, 0), P(0, 1), P(1, 0), P(0, -1),
P(-1, -1), P(-1, 1), P(1, -1), P(1, 1)};
inline P operator*(P::T m, P p) noexcept { return P(m * p.y, m * p.x); }
template <typename T>
// using T = int;
struct F {
int height, width;
vector<T> data;
F(int h = 1, int w = 1) : height(h), width(w), data(h * w) {}
inline T &operator()(int y, int x) { return data[x + y * width]; }
inline T &operator()(P p) { return data[p.x + p.y * width]; }
inline T operator()(int y, int x) const { return data[x + y * width]; }
inline T operator()(P p) const { return data[p.x + p.y * width]; }
inline bool safe(int y, int x) const {
return 0 <= y && y < height && 0 <= x && x < width;
}
inline bool safe(P p) const {
return 0 <= p.y && p.y < height && 0 <= p.x && p.x < width;
}
inline void fill(T e) { std::fill(all(data), e); }
inline void resize(int h, int w) {
height = h;
width = w;
data.resize(h * w);
}
void print(ostream &os, int setw_arg = 4) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x)
os << setw(setw_arg) << operator()(y, x) << ' ';
os << '\n';
}
}
};
// Edge構造体を定義する無向グラフ
class GraphE {
public:
using W_T = int;
struct Edge {
int u, v;
W_T value;
Edge(int from = 0, int to = 0, W_T value = 0)
: u(from), v(to), value(value) {}
inline int to(int _v) const { return _v == v ? u : v; }
};
size_t n;
vector<vector<int>> vertex_to;
vector<Edge> edges;
explicit GraphE(int n = 1) : n(n), vertex_to(n) {}
inline size_t size() const noexcept { return n; }
void resize(size_t _n) { vertex_to.resize(n = _n); }
void connect(int from, int to, W_T val = 0) {
vertex_to[(size_t)from].push_back((int)edges.size());
vertex_to[(size_t)to].push_back((int)edges.size());
edges.emplace_back(from, to, val);
}
void connect(vector<Edge> &&es) {
edges = move(es);
for (int i = 0; (size_t)i < edges.size(); ++i) {
vertex_to[edges[i].u].push_back(i);
vertex_to[edges[i].v].push_back(i);
}
}
};
//
int H, W, K;
int startx, starty, goalx, goaly;
//
F<int> field;
F<int> dist;
// GraphE graph;
inline int cv(int y, int x) { return y * W + x; }
int main() {
scanner >> H >> W >> K;
scanner >> starty >> startx >> goaly >> goalx;
--startx;
--starty;
--goalx;
--goaly;
field.resize(H, W);
// graph.resize(H*W);
repeat(i, H) {
string str;
scanner >> str;
repeat(j, W) {
char c = str[j];
field(i, j) = c == '@';
}
}
dist.resize(H, W);
dist.fill(1e9);
queue<P> que;
que.emplace(starty, startx);
dist(starty, startx) = 0;
while (!que.empty()) {
auto p = que.front();
que.pop();
auto d = dist(p);
if (p == P(goaly, goalx)) {
cout << d << endl;
exit(0); // memfree by os
}
for (auto v : FourMoving) {
auto p2 = v + p;
repeat(i, K) {
if (!field.safe(p2))
break;
if (field(p2))
break;
if (dist(p2) < d + 1)
break;
if (dist(p2) > d + 1) {
dist(p2) = d + 1;
que.push(p2);
}
p2 += v;
}
}
// repeat(i, H) {
// repeat(j, W) {
// cout << min(dist(i,j), 9);
// }
// cout << endl;
// }
// cout << endl;
}
cout << "-1" << endl;
exit(0);
return 0;
}
| replace | 353 | 354 | 353 | 354 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const long double pi = 3.141592653589793;
#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second
// #define COMMENT
/*
const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];
void inverse_modulao() {
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i = 2;i < NN;i++) {
fact[i] = fact[i-1]*i%mod;
inv[i] = mod-mod/i*inv[mod%i]%mod;
finv[i] = finv[i-1]*inv[i]%mod;
}
}
ll nCr(int n, int r) {
return fact[n]*finv[r]%mod*finv[n-r]%mod;
}
*/
int n, m, k, xi, yi, xj, yj;
string s[1 << 20];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
#define dist_state pair<int, pair<pii, int>>
int dijkstra() {
const int INF = 1e8;
vector<vector<vector<int>>> dist = vector<vector<vector<int>>>(
n + 3, vector<vector<int>>(m + 3, vector<int>(6, INF)));
set<pair<int, pair<pii, int>>> pq;
rep(i, 4) {
pq.insert({0, {{xi, yi}, i}});
dist[xi][yi][i] = 0;
}
while (!pq.empty()) {
dist_state ds = *pq.begin();
pq.erase(pq.begin());
int dxs = 4;
for (int i = 0; i < dxs; i++) {
if (!(dx[i] + dx[ds.S.S] == 0 && dy[i] + dy[ds.S.S] == 0)) {
int x = ds.S.F.F + dx[i], y = ds.S.F.S + dy[i], cnt = 1;
while (x >= 0 && y >= 0 && x < n && y < m && cnt <= k &&
s[x][y] == '.' && (ds.F + 1) <= dist[x][y][i]) {
pq.erase({dist[x][y][i], {{x, y}, i}});
pq.insert({1 + ds.F, {{x, y}, i}});
dist[x][y][i] = 1 + ds.F;
cnt++;
x += dx[i];
y += dy[i];
}
}
}
}
int ans = INF;
rep(i, 4) ans = min(ans, dist[xj][yj][i]);
return (ans == INF ? -1 : ans);
}
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
cin >> n >> m >> k;
cin >> xi >> yi >> xj >> yj;
xi--;
yi--;
xj--;
yj--;
rep(i, n) cin >> s[i];
cout << dijkstra() << endl;
return 0;
}
| #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const long double pi = 3.141592653589793;
#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, j, n) for (int i = j; i < n; i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second
// #define COMMENT
/*
const int NN = 200090;
ll fact[NN];
ll finv[NN];
ll inv[NN];
void inverse_modulao() {
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i = 2;i < NN;i++) {
fact[i] = fact[i-1]*i%mod;
inv[i] = mod-mod/i*inv[mod%i]%mod;
finv[i] = finv[i-1]*inv[i]%mod;
}
}
ll nCr(int n, int r) {
return fact[n]*finv[r]%mod*finv[n-r]%mod;
}
*/
int n, m, k, xi, yi, xj, yj;
string s[1 << 20];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
#define dist_state pair<int, pair<pii, int>>
int dijkstra() {
const int INF = 1e8;
vector<vector<vector<int>>> dist = vector<vector<vector<int>>>(
n + 3, vector<vector<int>>(m + 3, vector<int>(6, INF)));
set<pair<int, pair<pii, int>>> pq;
rep(i, 4) {
pq.insert({0, {{xi, yi}, i}});
dist[xi][yi][i] = 0;
}
while (!pq.empty()) {
dist_state ds = *pq.begin();
pq.erase(pq.begin());
int dxs = 4;
for (int i = 0; i < dxs; i++) {
if (!(dx[i] + dx[ds.S.S] == 0 && dy[i] + dy[ds.S.S] == 0)) {
int x = ds.S.F.F + dx[i], y = ds.S.F.S + dy[i], cnt = 1;
while (x >= 0 && y >= 0 && x < n && y < m && cnt <= k &&
s[x][y] == '.') {
if ((ds.F + 1) < dist[x][y][i]) {
pq.erase({dist[x][y][i], {{x, y}, i}});
pq.insert({1 + ds.F, {{x, y}, i}});
dist[x][y][i] = 1 + ds.F;
} else if ((ds.F + 1) > dist[x][y][i])
break;
cnt++;
x += dx[i];
y += dy[i];
}
}
}
}
int ans = INF;
rep(i, 4) ans = min(ans, dist[xj][yj][i]);
return (ans == INF ? -1 : ans);
}
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
cin >> n >> m >> k;
cin >> xi >> yi >> xj >> yj;
xi--;
yi--;
xj--;
yj--;
rep(i, n) cin >> s[i];
cout << dijkstra() << endl;
return 0;
}
| replace | 95 | 99 | 95 | 102 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
char e[h + 2][w + 2];
for (int i = 0; i < w + 2; i++) {
e[0][i] = '@';
e[h + 1][i] = '@';
}
for (int i = 1; i < h + 1; i++) {
e[i][0] = '@';
for (int j = 1; j < w + 1; j++)
cin >> e[i][j];
e[i][w + 1] = '@';
}
ll dist[h + 2][w + 2];
memset(dist, -1, sizeof(dist));
dist[sx][sy] = 0;
queue<pair<ll, ll>> que;
que.push(make_pair(sx, sy));
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
while (!que.empty()) {
ll y = que.front().first;
ll x = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
if (y + dy[i] * j < 0 || y + dy[i] * j > h + 1 || x + dx[i] * j < 0 ||
x + dx[i] * j > w + 1)
break;
if (e[y + dy[i] * j][x + dx[i] * j] == '@')
break;
if (dist[y + dy[i] * j][x + dx[i] * j] == -1) {
dist[y + dy[i] * j][x + dx[i] * j] = dist[y][x] + 1;
que.push(make_pair(y + dy[i] * j, x + dx[i] * j));
}
}
}
}
cout << dist[gx][gy] << endl;
} | #include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
char e[h + 2][w + 2];
for (int i = 0; i < w + 2; i++) {
e[0][i] = '@';
e[h + 1][i] = '@';
}
for (int i = 1; i < h + 1; i++) {
e[i][0] = '@';
for (int j = 1; j < w + 1; j++)
cin >> e[i][j];
e[i][w + 1] = '@';
}
ll dist[h + 2][w + 2];
memset(dist, -1, sizeof(dist));
dist[sx][sy] = 0;
queue<pair<ll, ll>> que;
que.push(make_pair(sx, sy));
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
while (!que.empty()) {
ll y = que.front().first;
ll x = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
if (y + dy[i] * j < 0 || y + dy[i] * j > h + 1 || x + dx[i] * j < 0 ||
x + dx[i] * j > w + 1)
break;
if (e[y + dy[i] * j][x + dx[i] * j] == '@')
break;
if (dist[y + dy[i] * j][x + dx[i] * j] != -1) {
if (dist[y + dy[i] * j][x + dx[i] * j] > dist[y][x])
;
else
break;
}
if (dist[y + dy[i] * j][x + dx[i] * j] == -1) {
dist[y + dy[i] * j][x + dx[i] * j] = dist[y][x] + 1;
que.push(make_pair(y + dy[i] * j, x + dx[i] * j));
}
}
}
}
cout << dist[gx][gy] << endl;
} | insert | 50 | 50 | 50 | 56 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define INF (1 << 28)
using namespace std;
int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
void solve() {
queue<P> q;
q.push(P(x1, y1));
int d[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
d[x1][y1] = 0;
while (!q.empty()) {
P cc = q.front();
q.pop();
int cx = cc.first, cy = cc.second;
// printf("c = (%d, %d)\n", cx, cy);
if (cx == x2 && cy == y2) {
break;
}
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
// printf("n = (%d, %d) = %c\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
// && ny < W ? c[nx][ny] : '-'));
if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
d[nx][ny] > d[cx][cy]) {
d[nx][ny] = d[cx][cy] + 1;
q.push(P(nx, ny));
} else {
break;
}
}
}
}
printf("%d\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}
int main() {
scanf("%d %d %d", &H, &W, &K);
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1--;
y1--;
x2--;
y2--;
getchar();
for (int i = 0; i < H; i++) {
c.push_back(vector<char>());
for (int j = 0; j < W; j++) {
c[i].push_back(getchar());
}
getchar();
}
solve();
}
| #include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define INF (1 << 28)
using namespace std;
int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
void solve() {
queue<P> q;
q.push(P(x1, y1));
int d[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
d[i][j] = INF;
}
}
d[x1][y1] = 0;
while (!q.empty()) {
P cc = q.front();
q.pop();
int cx = cc.first, cy = cc.second;
// printf("c = (%d, %d)\n", cx, cy);
if (cx == x2 && cy == y2) {
break;
}
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
// printf("n = (%d, %d) = %c\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
// && ny < W ? c[nx][ny] : '-'));
if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
d[nx][ny] > d[cx][cy]) {
if (d[nx][ny] > d[cx][cy] + 1) {
d[nx][ny] = d[cx][cy] + 1;
q.push(P(nx, ny));
}
} else {
break;
}
}
}
}
printf("%d\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}
int main() {
scanf("%d %d %d", &H, &W, &K);
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1--;
y1--;
x2--;
y2--;
getchar();
for (int i = 0; i < H; i++) {
c.push_back(vector<char>());
for (int j = 0; j < W; j++) {
c[i].push_back(getchar());
}
getchar();
}
solve();
}
| replace | 44 | 46 | 44 | 48 | TLE | |
p02644 | C++ | Time Limit Exceeded | // https://atcoder.jp/contests/abc170/tasks/abc170_f
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 0
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a) \
REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b) \
REP(_j, b.size()) DUMP(b[_j]); \
cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif
const int H_MAX = 1e6;
const int W_MAX = 1e6;
const int K_MAX = 1e6;
const int INF = 1e9;
const int D[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
using P = pair<int, int>;
int H, W, K;
int x_1, y_1, x_2, y_2;
vector<string> c;
vector<vector<int>> d;
int bfs() {
queue<P> que;
que.push({x_1, y_1});
d = vector<vector<int>>(H, vector<int>(W, INF));
d[x_1][y_1] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int i = p.first;
int j = p.second;
if (i == x_2 && j == y_2)
break;
REP(k, 4) {
int i2 = i;
int j2 = j;
FOR(l, 1, K) {
i2 += D[k][0];
j2 += D[k][1];
if (i2 < 0 || i2 >= H || j2 < 0 || j2 >= W)
break;
if (c[i2][j2] == '@')
break;
if (d[i2][j2] < INF)
continue;
que.push({i2, j2});
d[i2][j2] = d[i][j] + 1;
}
}
}
return d[x_2][y_2];
}
int main() {
cin >> H >> W >> K;
cin >> x_1 >> y_1 >> x_2 >> y_2;
x_1--;
y_1--;
x_2--;
y_2--;
c = vector<string>(H);
REP(i, H) cin >> c[i];
int ans = bfs();
DUMP2D(d);
if (ans < INF)
cout << ans << endl;
else
cout << -1 << endl;
}
| // https://atcoder.jp/contests/abc170/tasks/abc170_f
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 0
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a) \
REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b) \
REP(_j, b.size()) DUMP(b[_j]); \
cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif
const int H_MAX = 1e6;
const int W_MAX = 1e6;
const int K_MAX = 1e6;
const int INF = 1e9;
const int D[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
using P = pair<int, int>;
int H, W, K;
int x_1, y_1, x_2, y_2;
vector<string> c;
vector<vector<int>> d;
int bfs() {
queue<P> que;
que.push({x_1, y_1});
d = vector<vector<int>>(H, vector<int>(W, INF));
d[x_1][y_1] = 0;
while (!que.empty()) {
P p = que.front();
que.pop();
int i = p.first;
int j = p.second;
if (i == x_2 && j == y_2)
break;
REP(k, 4) {
int i2 = i;
int j2 = j;
FOR(l, 1, K) {
i2 += D[k][0];
j2 += D[k][1];
if (i2 < 0 || i2 >= H || j2 < 0 || j2 >= W)
break;
if (c[i2][j2] == '@' || d[i2][j2] <= d[i][j])
break;
if (d[i2][j2] < INF)
continue;
que.push({i2, j2});
d[i2][j2] = d[i][j] + 1;
}
}
}
return d[x_2][y_2];
}
int main() {
cin >> H >> W >> K;
cin >> x_1 >> y_1 >> x_2 >> y_2;
x_1--;
y_1--;
x_2--;
y_2--;
c = vector<string>(H);
REP(i, H) cin >> c[i];
int ans = bfs();
DUMP2D(d);
if (ans < INF)
cout << ans << endl;
else
cout << -1 << endl;
}
| replace | 53 | 54 | 53 | 54 | TLE | |
p02644 | C++ | Time Limit Exceeded | /*
Author : Simanta Deb Turja
*/
#include <bits/stdc++.h>
using namespace std;
#define Professor
using ll = long long;
using i64 = unsigned long long;
template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); }
template <typename T> inline T Min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); }
template <typename T> inline T Max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
template <typename T> inline T Ceil(T a, T b) {
return ((a % b == 0) ? (a / b) : (a / b + 1));
}
template <typename T> inline T Floor(T a, T b) { return a / b; }
template <typename T> inline T Power(T a, T p) {
T res = 1, x = a;
while (p) {
if (p & 1)
res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
template <typename T> inline T BigMod(T a, T p, T M) {
a %= M;
T ret = (T)1;
while (p) {
if (p & 1)
ret = (ret * a) % M;
a = (a * a) % M;
p = p >> 1;
}
return ret;
}
template <typename T> inline T InverseMod(T a, T M) {
return BigMod(a, M - 2, M);
}
template <typename T> inline T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T> inline T lcm(T x, T y) {
return (((x) / gcd((x), (y))) * (y));
}
template <typename T> inline T Reverse(T x) {
reverse(x.begin(), x.end());
return x;
}
template <typename T1, typename T2, typename T3> struct Pair {
T1 first;
T2 second;
T3 third;
};
template <typename T1, typename T2, typename T3, typename T4> struct Pair4 {
T1 first;
T2 second;
T3 third;
T4 fourth;
};
using ii = pair<int, int>;
using iii = Pair<int, int, int>;
using iiii = Pair4<int, int, int, int>;
#define endl '\n'
#define all(x) x.begin(), x.end()
#define all_c(x, c) x.begin(), x.end(), c
const int N = (int)4e5 + 10;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const ll LLINF = (ll)1e18 + 1;
const int INF = (int)1e9 + 1;
const ll MOD = (ll)1e9 + 7;
template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) {
return a.first < b.first;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
auto Solve = [&]() {
#ifdef __APPLE__
cout << "Started Running.....\n";
#endif
int h, w, k_max, sx, sy, ex, ey;
cin >> h >> w >> k_max >> sx >> sy >> ex >> ey;
--sx, --sy, --ex, --ey;
vector<vector<char>> grid(h, vector<char>(w));
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> grid[i][j];
}
}
vector<vector<int>> dist(h, vector<int>(w, -1));
function<bool(int, int)> isValid = [&](int x, int y) {
return (x >= 0 && x < h && y >= 0 && y < w);
};
function<void(void)> bfs = [&](void) {
dist[sx][sy] = 0;
queue<ii> q;
q.push({sx, sy});
while (!q.empty()) {
auto current = q.front();
q.pop();
int cx = current.first, cy = current.second;
for (int i = 0; i < 4; ++i) {
for (int k = 1; k <= k_max; ++k) {
int nx = cx + (dx[i] * k);
int ny = cy + (dy[i] * k);
if (!isValid(nx, ny) || grid[nx][ny] == '@')
break;
if (isValid(nx, ny) && grid[nx][ny] != '@') {
if (dist[nx][ny] == -1) {
dist[nx][ny] = dist[cx][cy] + 1;
q.push({nx, ny});
} else {
if (dist[nx][ny] >= dist[cx][cy] + 1) {
q.push({nx, ny});
dist[nx][ny] = dist[cx][cy] + 1;
} else {
break;
}
}
}
}
}
}
};
bfs();
if (dist[ex][ey] != -1) {
cout << dist[ex][ey] << endl;
} else {
cout << "-1\n";
}
};
Solve();
return 0;
}
| /*
Author : Simanta Deb Turja
*/
#include <bits/stdc++.h>
using namespace std;
#define Professor
using ll = long long;
using i64 = unsigned long long;
template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); }
template <typename T> inline T Min(T a, T b, T c, T d) {
return min(a, min(b, min(c, d)));
}
template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); }
template <typename T> inline T Max(T a, T b, T c, T d) {
return max(a, max(b, max(c, d)));
}
template <typename T> inline T Ceil(T a, T b) {
return ((a % b == 0) ? (a / b) : (a / b + 1));
}
template <typename T> inline T Floor(T a, T b) { return a / b; }
template <typename T> inline T Power(T a, T p) {
T res = 1, x = a;
while (p) {
if (p & 1)
res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
template <typename T> inline T BigMod(T a, T p, T M) {
a %= M;
T ret = (T)1;
while (p) {
if (p & 1)
ret = (ret * a) % M;
a = (a * a) % M;
p = p >> 1;
}
return ret;
}
template <typename T> inline T InverseMod(T a, T M) {
return BigMod(a, M - 2, M);
}
template <typename T> inline T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T> inline T lcm(T x, T y) {
return (((x) / gcd((x), (y))) * (y));
}
template <typename T> inline T Reverse(T x) {
reverse(x.begin(), x.end());
return x;
}
template <typename T1, typename T2, typename T3> struct Pair {
T1 first;
T2 second;
T3 third;
};
template <typename T1, typename T2, typename T3, typename T4> struct Pair4 {
T1 first;
T2 second;
T3 third;
T4 fourth;
};
using ii = pair<int, int>;
using iii = Pair<int, int, int>;
using iiii = Pair4<int, int, int, int>;
#define endl '\n'
#define all(x) x.begin(), x.end()
#define all_c(x, c) x.begin(), x.end(), c
const int N = (int)4e5 + 10;
const double EPS = 1e-7;
const double PI = acos(-1.0);
const ll LLINF = (ll)1e18 + 1;
const int INF = (int)1e9 + 1;
const ll MOD = (ll)1e9 + 7;
template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) {
return a.first < b.first;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
auto Solve = [&]() {
#ifdef __APPLE__
cout << "Started Running.....\n";
#endif
int h, w, k_max, sx, sy, ex, ey;
cin >> h >> w >> k_max >> sx >> sy >> ex >> ey;
--sx, --sy, --ex, --ey;
vector<vector<char>> grid(h, vector<char>(w));
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> grid[i][j];
}
}
vector<vector<int>> dist(h, vector<int>(w, -1));
function<bool(int, int)> isValid = [&](int x, int y) {
return (x >= 0 && x < h && y >= 0 && y < w);
};
function<void(void)> bfs = [&](void) {
dist[sx][sy] = 0;
queue<ii> q;
q.push({sx, sy});
while (!q.empty()) {
auto current = q.front();
q.pop();
int cx = current.first, cy = current.second;
for (int i = 0; i < 4; ++i) {
for (int k = 1; k <= k_max; ++k) {
int nx = cx + (dx[i] * k);
int ny = cy + (dy[i] * k);
if (!isValid(nx, ny) || grid[nx][ny] == '@')
break;
if (isValid(nx, ny) && grid[nx][ny] != '@') {
if (dist[nx][ny] == -1) {
dist[nx][ny] = dist[cx][cy] + 1;
q.push({nx, ny});
} else {
if (dist[nx][ny] >= dist[cx][cy] + 1) {
dist[nx][ny] = dist[cx][cy] + 1;
} else {
break;
}
}
}
}
}
}
};
bfs();
if (dist[ex][ey] != -1) {
cout << dist[ex][ey] << endl;
} else {
cout << "-1\n";
}
};
Solve();
return 0;
}
| delete | 142 | 143 | 142 | 142 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll mmod = 998244353;
vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};
vector<string> g;
vector<vector<bool>> used;
vector<ll> d;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
ll h, w, k, sx, sy, ex, ey;
cin >> h >> w >> k >> sx >> sy >> ex >> ey;
g = vector<string>(h);
used = vector<vector<bool>>(h, vector<bool>(w));
for (int i = 0; i < h; ++i) {
cin >> g[i];
}
sx--;
sy--;
ex--;
ey--;
P s = P(sx, sy);
vector<vector<ll>> dist(h, vector<ll>(w, mod));
dist[sx][sy] = 0;
queue<P> q;
q.push(s);
while (q.size() > 0) {
P cur = q.front();
q.pop();
if (used[cur.first][cur.second]) {
continue;
}
used[cur.first][cur.second] = 1;
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
ll nx = cur.first + dx[i] * j;
ll ny = cur.second + dy[i] * j;
if (nx >= 0 && ny >= 0 && nx < h && ny < w && !used[nx][ny]) {
if (g[nx][ny] == '@') {
break;
}
if (dist[nx][ny] <= dist[cur.first][cur.second]) {
break;
}
dist[nx][ny] = dist[cur.first][cur.second] + 1;
q.push(P(nx, ny));
}
}
}
if (dist[ex][ey] != mod) {
cout << dist[ex][ey] << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll mmod = 998244353;
vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};
vector<string> g;
vector<vector<bool>> used;
vector<ll> d;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
ll h, w, k, sx, sy, ex, ey;
cin >> h >> w >> k >> sx >> sy >> ex >> ey;
g = vector<string>(h);
used = vector<vector<bool>>(h, vector<bool>(w));
for (int i = 0; i < h; ++i) {
cin >> g[i];
}
sx--;
sy--;
ex--;
ey--;
P s = P(sx, sy);
vector<vector<ll>> dist(h, vector<ll>(w, mod));
dist[sx][sy] = 0;
queue<P> q;
q.push(s);
while (q.size() > 0) {
P cur = q.front();
q.pop();
if (used[cur.first][cur.second]) {
continue;
}
used[cur.first][cur.second] = 1;
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
ll nx = cur.first + dx[i] * j;
ll ny = cur.second + dy[i] * j;
if (nx >= 0 && ny >= 0 && nx < h && ny < w) {
if (used[nx][ny]) {
break;
}
if (g[nx][ny] == '@') {
break;
}
if (dist[nx][ny] <= dist[cur.first][cur.second]) {
break;
}
dist[nx][ny] = dist[cur.first][cur.second] + 1;
q.push(P(nx, ny));
}
}
}
if (dist[ex][ey] != mod) {
cout << dist[ex][ey] << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} | replace | 63 | 64 | 63 | 67 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
int h, w, k;
cin >> h >> w >> k;
vector<int> s(4);
rep(i, 4) {
cin >> s[i];
s[i]--;
}
vector<vector<char>> c(h, vector<char>(w));
rep(i, h) rep(j, w) cin >> c[i][j];
using pi = pair<int, int>;
using pii = pair<int, pi>;
queue<pii> q;
q.push({0, {s[0], s[1]}});
vector<vector<int>> d(h, vector<int>(w, -1));
d[s[0]][s[1]] = 0;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
while (!q.empty()) {
pii p = q.front();
q.pop();
int dd = p.first;
int x = p.second.first, y = p.second.second;
rep(i, 4) {
rep1(j, k) {
int nx = x + dx[i] * j;
int ny = y + dy[i] * j;
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (c[nx][ny] == '@')
break;
if (0 <= d[nx][ny] && d[nx][ny] <= d[x][y])
break;
d[nx][ny] = d[x][y] + 1;
q.push({d[nx][ny], {nx, ny}});
} else
break;
}
}
}
// rep(i,h) {
// rep(j,w) cout << d[i][j] << " ";
// cout << "\n";
// }
cout << d[s[2]][s[3]] << "\n";
return 0;
}
| #include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
int h, w, k;
cin >> h >> w >> k;
vector<int> s(4);
rep(i, 4) {
cin >> s[i];
s[i]--;
}
vector<vector<char>> c(h, vector<char>(w));
rep(i, h) rep(j, w) cin >> c[i][j];
using pi = pair<int, int>;
using pii = pair<int, pi>;
queue<pii> q;
q.push({0, {s[0], s[1]}});
vector<vector<int>> d(h, vector<int>(w, -1));
d[s[0]][s[1]] = 0;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
while (!q.empty()) {
pii p = q.front();
q.pop();
int dd = p.first;
int x = p.second.first, y = p.second.second;
rep(i, 4) {
rep1(j, k) {
int nx = x + dx[i] * j;
int ny = y + dy[i] * j;
if (0 <= nx && nx < h && 0 <= ny && ny < w) {
if (c[nx][ny] == '@')
break;
if (d[nx][ny] == d[x][y] + 1)
continue;
if (0 <= d[nx][ny])
break;
d[nx][ny] = d[x][y] + 1;
q.push({d[nx][ny], {nx, ny}});
} else
break;
}
}
}
// rep(i,h) {
// rep(j,w) cout << d[i][j] << " ";
// cout << "\n";
// }
cout << d[s[2]][s[3]] << "\n";
return 0;
}
| replace | 53 | 54 | 53 | 56 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;
vector<int> dx{1, 0, -1, 0};
vector<int> dy{0, 1, 0, -1};
int main() {
int H, W, K;
cin >> H >> W >> K;
pair<int, int> start, goal;
cin >> start.first >> start.second >> goal.first >> goal.second;
vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
FOR(i, 1, H + 1) {
string S;
cin >> S;
FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
}
G[start.first][start.second] = 0;
queue<pair<int, int>> q;
q.push(start);
while (!q.empty()) {
auto pos = q.front();
q.pop();
if (pos == goal) {
cout << G[pos.first][pos.second] << endl;
return 0;
}
REP(i, 4) {
FOR(k, 1, K + 1) {
int next_x = pos.first + k * dx[i];
int next_y = pos.second + k * dy[i];
if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
// 水上かつ未探索なら
G[next_x][next_y] = G[pos.first][pos.second] + 1;
q.push(make_pair(next_x, next_y));
} else if (pond[next_x][next_y] == '@') {
break;
}
}
}
}
cout << -1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;
vector<int> dx{1, 0, -1, 0};
vector<int> dy{0, 1, 0, -1};
int main() {
int H, W, K;
cin >> H >> W >> K;
pair<int, int> start, goal;
cin >> start.first >> start.second >> goal.first >> goal.second;
vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
FOR(i, 1, H + 1) {
string S;
cin >> S;
FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
}
G[start.first][start.second] = 0;
queue<pair<int, int>> q;
q.push(start);
while (!q.empty()) {
auto pos = q.front();
q.pop();
if (pos == goal) {
cout << G[pos.first][pos.second] << endl;
return 0;
}
REP(i, 4) {
FOR(k, 1, K + 1) {
int next_x = pos.first + k * dx[i];
int next_y = pos.second + k * dy[i];
if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
// 水上かつ未探索なら
G[next_x][next_y] = G[pos.first][pos.second] + 1;
q.push(make_pair(next_x, next_y));
} else if (pond[next_x][next_y] == '@' ||
G[next_x][next_y] <= G[pos.first][pos.second]) {
break;
}
}
}
}
cout << -1 << endl;
return 0;
} | replace | 47 | 48 | 47 | 49 | TLE | |
p02644 | C++ | Runtime Error | #include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return 0;
}
using P = pair<int, int>;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
vector<string> grid(H);
for (auto &g : grid) {
cin >> g;
}
vector<vector<int>> dp(H, vector<int>(W, INF));
queue<P> que;
que.emplace(x1, y1);
dp[x1][y1] = 0;
vector<int> dh = {-1, 0, 1, 0};
vector<int> dw = {0, -1, 0, 1};
while (!que.empty()) {
P now = que.front();
que.pop();
int h = now.first;
int w = now.second;
rep(i, 0, 4) {
int nh = h;
int nw = w;
rep(j, 0, K) {
nh += dh[i];
nw += dw[i];
if (nh < 0 || nh >= H || nw < 0 || nh >= W) {
break;
}
if (grid[nh][nw] == '@') {
break;
}
if (dp[nh][nw] <= dp[h][w]) {
break;
}
dp[nh][nw] = dp[h][w] + 1;
que.emplace(nh, nw);
}
}
}
if (dp[x2][y2] == INF) {
cout << -1 << endl;
} else {
cout << dp[x2][y2] << endl;
}
return 0;
} | #include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return 0;
}
using P = pair<int, int>;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
vector<string> grid(H);
for (auto &g : grid) {
cin >> g;
}
vector<vector<int>> dp(H, vector<int>(W, INF));
queue<P> que;
que.emplace(x1, y1);
dp[x1][y1] = 0;
vector<int> dh = {-1, 0, 1, 0};
vector<int> dw = {0, -1, 0, 1};
while (!que.empty()) {
P now = que.front();
que.pop();
int h = now.first;
int w = now.second;
rep(i, 0, 4) {
int nh = h;
int nw = w;
rep(j, 0, K) {
nh += dh[i];
nw += dw[i];
if (nh < 0 || nh >= H || nw < 0 || nh >= W) {
break;
}
if (grid[nh][nw] == '@') {
break;
}
if (dp[nh][nw] <= dp[h][w]) {
break;
}
if (dp[nh][nw] == INF) {
dp[nh][nw] = dp[h][w] + 1;
que.emplace(nh, nw);
}
}
}
}
if (dp[x2][y2] == INF) {
cout << -1 << endl;
} else {
cout << dp[x2][y2] << endl;
}
return 0;
} | replace | 66 | 68 | 66 | 70 | 0 | |
p02644 | C++ | Time Limit Exceeded | /*
ここの解説を元にACした: https://betrue12.hateblo.jp/entry/2020/06/14/232948
*/
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return 0;
}
using state = tuple<int, int, int>;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
vector<string> grid(H);
for (auto &g : grid) {
cin >> g;
}
priority_queue<state, vector<state>, greater<state>> que;
vector<vector<int>> dp(H, vector<int>(W, INF));
vector<int> dh = {-1, 0, 1, 0};
vector<int> dw = {0, -1, 0, 1};
dp[x1][y1] = 0;
que.emplace(0, x1, y1);
while (!que.empty()) {
state now = que.top();
que.pop();
int dist = get<0>(now);
int h = get<1>(now);
int w = get<2>(now);
if (dist > dp[h][w]) {
continue;
}
rep(i, 0, 4) {
int nh = h;
int nw = w;
rep(_, 0, K) {
nh += dh[i];
nw += dw[i];
if (nh < 0 || nh >= H || nw < 0 || nw >= W || grid[nh][nw] == '@') {
break;
}
if (dp[nh][nw] <= dist) {
break;
}
dp[nh][nw] = dist + 1;
que.emplace(dp[nh][nw], nh, nw);
}
}
}
if (dp[x2][y2] == INF) {
cout << -1 << endl;
} else {
cout << dp[x2][y2] << endl;
}
return 0;
} | /*
ここの解説を元にACした: https://betrue12.hateblo.jp/entry/2020/06/14/232948
*/
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return 0;
}
using state = tuple<int, int, int>;
const int INF = 1 << 30;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
vector<string> grid(H);
for (auto &g : grid) {
cin >> g;
}
priority_queue<state, vector<state>, greater<state>> que;
vector<vector<int>> dp(H, vector<int>(W, INF));
vector<int> dh = {-1, 0, 1, 0};
vector<int> dw = {0, -1, 0, 1};
dp[x1][y1] = 0;
que.emplace(0, x1, y1);
while (!que.empty()) {
state now = que.top();
que.pop();
int dist = get<0>(now);
int h = get<1>(now);
int w = get<2>(now);
if (dist > dp[h][w]) {
continue;
}
rep(i, 0, 4) {
int nh = h;
int nw = w;
rep(_, 0, K) {
nh += dh[i];
nw += dw[i];
if (nh < 0 || nh >= H || nw < 0 || nw >= W || grid[nh][nw] == '@') {
break;
}
if (dp[nh][nw] <= dist) {
break;
}
if (chmin(dp[nh][nw], dist + 1)) {
que.emplace(dp[nh][nw], nh, nw);
}
}
}
}
if (dp[x2][y2] == INF) {
cout << -1 << endl;
} else {
cout << dp[x2][y2] << endl;
}
return 0;
} | replace | 71 | 73 | 71 | 74 | TLE | |
p02644 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ppp pair<pii, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define CL(x) memset(x, 0, sizeof(x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
#define DI(x) (int)(x - '0')
#define LL_MAX (1LL << 60)
#define DB 0
#define DBG(x) \
if (DB) \
cout << #x << " : " << x << '\n'
#define PRALL(v) \
if (DB) \
for (auto it : v) \
cout << v << ' '; \
NL;
#define NL cout << '\n';
#define FND(S, x) (S.find(x) != S.end())
#define bit(x, y) (!!(x & (1 << y)))
typedef long long ll;
typedef long double ld;
// Grid, BFS, Set
#define MXN 200005
int R, C, K;
int X1, Y1, X2, Y2;
set<int> Rpos[MXN], Cpos[MXN]; // unvisited pos
set<int> RB[MXN], CB[MXN]; // blocks
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> R >> C >> K;
cin >> X1 >> Y1 >> X2 >> Y2;
string str;
vector<vector<int>> dis(R + 1, vector<int>(C + 1, 1e9));
FOR(i, 1, R) {
RB[i].insert(-1);
RB[i].insert(C + 1);
Rpos[i].insert(-2);
Rpos[i].insert(C + 2);
}
FOR(i, 1, C) {
CB[i].insert(-1);
CB[i].insert(R + 1);
Cpos[i].insert(-2);
Cpos[i].insert(R + 2);
}
FOR(i, 1, R) {
cin >> str;
FOR(j, 1, C) {
if (str[j - 1] == '@') {
RB[i].insert(j);
CB[j].insert(i);
} else {
Rpos[i].insert(j);
Cpos[j].insert(i);
}
}
}
// BFS
dis[X1][Y1] = 0;
Rpos[X1].erase(Y1);
Cpos[Y1].erase(X1);
queue<pii> Q;
Q.push(mkp(X1, Y1));
while (!Q.empty()) {
pii pnow = Q.front();
Q.pop();
int x = pnow.f, y = pnow.s;
int d = dis[x][y];
// cout<<x<<' '<<y<<' '<<d<<'\n';
int lb, rb, ub, db;
// going LR
auto ptr = RB[x].lower_bound(y);
rb = *ptr;
ptr--;
lb = *ptr;
vector<int> dlist;
// going L
ptr = Rpos[x].lower_bound(y);
FOR(i, 1, K) {
ptr--;
int y2 = *ptr;
if (y - y2 > K || y2 < lb)
break;
dis[x][y2] = d + 1;
Q.push(mkp(x, y2));
dlist.pb(y2);
}
// going R
ptr = Rpos[x].lower_bound(y);
FOR(i, 1, K) {
int y2 = *ptr;
if (y2 - y > K || y2 > rb)
break;
dis[x][y2] = d + 1;
Q.push(mkp(x, y2));
dlist.pb(y2);
ptr++;
}
// cleaning
for (int yi : dlist) {
// cout<<"cleaning "<<x<<' '<<yi<<'\n';
Cpos[yi].erase(x);
Rpos[x].erase(yi);
}
dlist.clear();
// going UD
ptr = CB[y].lower_bound(x);
db = *ptr;
ptr--;
ub = *ptr;
// going U
ptr = Cpos[y].lower_bound(x);
FOR(i, 1, K) {
ptr--;
int x2 = *ptr;
if (x - x2 > K || x2 < ub)
break;
dis[x2][y] = d + 1;
Q.push(mkp(x2, y));
dlist.pb(x2);
}
// going R
ptr = Cpos[y].lower_bound(x);
FOR(i, 1, K) {
int x2 = *ptr;
if (x2 - x > K || x2 > db)
break;
dis[x2][y] = d + 1;
Q.push(mkp(x2, y));
;
dlist.pb(x2);
ptr++;
}
// cleaning
for (int xi : dlist) {
// cout<<"cleaning "<<xi<<' '<<y<<'\n';
Rpos[xi].erase(y);
Cpos[y].erase(xi);
}
}
if (dis[X2][Y2] == 1e9)
cout << -1 << '\n';
else
cout << dis[X2][Y2] << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define ppp pair<pii, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define CL(x) memset(x, 0, sizeof(x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
#define DI(x) (int)(x - '0')
#define LL_MAX (1LL << 60)
#define DB 0
#define DBG(x) \
if (DB) \
cout << #x << " : " << x << '\n'
#define PRALL(v) \
if (DB) \
for (auto it : v) \
cout << v << ' '; \
NL;
#define NL cout << '\n';
#define FND(S, x) (S.find(x) != S.end())
#define bit(x, y) (!!(x & (1 << y)))
typedef long long ll;
typedef long double ld;
// Grid, BFS, Set
#define MXN 1000005
int R, C, K;
int X1, Y1, X2, Y2;
set<int> Rpos[MXN], Cpos[MXN]; // unvisited pos
set<int> RB[MXN], CB[MXN]; // blocks
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> R >> C >> K;
cin >> X1 >> Y1 >> X2 >> Y2;
string str;
vector<vector<int>> dis(R + 1, vector<int>(C + 1, 1e9));
FOR(i, 1, R) {
RB[i].insert(-1);
RB[i].insert(C + 1);
Rpos[i].insert(-2);
Rpos[i].insert(C + 2);
}
FOR(i, 1, C) {
CB[i].insert(-1);
CB[i].insert(R + 1);
Cpos[i].insert(-2);
Cpos[i].insert(R + 2);
}
FOR(i, 1, R) {
cin >> str;
FOR(j, 1, C) {
if (str[j - 1] == '@') {
RB[i].insert(j);
CB[j].insert(i);
} else {
Rpos[i].insert(j);
Cpos[j].insert(i);
}
}
}
// BFS
dis[X1][Y1] = 0;
Rpos[X1].erase(Y1);
Cpos[Y1].erase(X1);
queue<pii> Q;
Q.push(mkp(X1, Y1));
while (!Q.empty()) {
pii pnow = Q.front();
Q.pop();
int x = pnow.f, y = pnow.s;
int d = dis[x][y];
// cout<<x<<' '<<y<<' '<<d<<'\n';
int lb, rb, ub, db;
// going LR
auto ptr = RB[x].lower_bound(y);
rb = *ptr;
ptr--;
lb = *ptr;
vector<int> dlist;
// going L
ptr = Rpos[x].lower_bound(y);
FOR(i, 1, K) {
ptr--;
int y2 = *ptr;
if (y - y2 > K || y2 < lb)
break;
dis[x][y2] = d + 1;
Q.push(mkp(x, y2));
dlist.pb(y2);
}
// going R
ptr = Rpos[x].lower_bound(y);
FOR(i, 1, K) {
int y2 = *ptr;
if (y2 - y > K || y2 > rb)
break;
dis[x][y2] = d + 1;
Q.push(mkp(x, y2));
dlist.pb(y2);
ptr++;
}
// cleaning
for (int yi : dlist) {
// cout<<"cleaning "<<x<<' '<<yi<<'\n';
Cpos[yi].erase(x);
Rpos[x].erase(yi);
}
dlist.clear();
// going UD
ptr = CB[y].lower_bound(x);
db = *ptr;
ptr--;
ub = *ptr;
// going U
ptr = Cpos[y].lower_bound(x);
FOR(i, 1, K) {
ptr--;
int x2 = *ptr;
if (x - x2 > K || x2 < ub)
break;
dis[x2][y] = d + 1;
Q.push(mkp(x2, y));
dlist.pb(x2);
}
// going R
ptr = Cpos[y].lower_bound(x);
FOR(i, 1, K) {
int x2 = *ptr;
if (x2 - x > K || x2 > db)
break;
dis[x2][y] = d + 1;
Q.push(mkp(x2, y));
;
dlist.pb(x2);
ptr++;
}
// cleaning
for (int xi : dlist) {
// cout<<"cleaning "<<xi<<' '<<y<<'\n';
Rpos[xi].erase(y);
Cpos[y].erase(xi);
}
}
if (dis[X2][Y2] == 1e9)
cout << -1 << '\n';
else
cout << dis[X2][Y2] << '\n';
return 0;
}
| replace | 42 | 43 | 42 | 43 | 0 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;
int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
cin >> H >> W >> K;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
rep(i, H) {
c[i].resize(W);
dis[i].resize(W, inf);
}
rep(i, H) rep(j, W) cin >> c[i][j];
queue<P> que;
que.push({sx, sy});
dis[sx][sy] = 0;
while (!que.empty()) {
P q = que.front();
que.pop();
rep(i, 4) {
REP(j, K + 1) {
int nx = q.first + dx[i] * j;
int ny = q.second + dy[i] * j;
if (nx < 0 || ny < 0 || nx >= H || ny >= W)
break;
if (c[nx][ny] == '@')
break;
int ne = dis[q.first][q.second] + 1;
if (dis[nx][ny] < ne)
break;
dis[nx][ny] = ne;
que.push({nx, ny});
}
}
}
/*rep(i,H){
rep(j,W)cout<<dis[i][j]<<" ";
cout<<endl;
}*/
if (dis[gx][gy] == inf)
cout << -1 << endl;
else
cout << dis[gx][gy] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;
int H, W, K;
int sx, sy, gx, gy;
vector<char> c[1000005];
vector<int> dis[1000005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
cin >> H >> W >> K;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
rep(i, H) {
c[i].resize(W);
dis[i].resize(W, inf);
}
rep(i, H) rep(j, W) cin >> c[i][j];
queue<P> que;
que.push({sx, sy});
dis[sx][sy] = 0;
while (!que.empty()) {
P q = que.front();
que.pop();
rep(i, 4) {
REP(j, K + 1) {
int nx = q.first + dx[i] * j;
int ny = q.second + dy[i] * j;
if (nx < 0 || ny < 0 || nx >= H || ny >= W)
break;
if (c[nx][ny] == '@')
break;
int ne = dis[q.first][q.second] + 1;
if (dis[nx][ny] < ne)
break;
if (dis[nx][ny] > ne) {
dis[nx][ny] = ne;
que.push({nx, ny});
}
}
}
}
/*rep(i,H){
rep(j,W)cout<<dis[i][j]<<" ";
cout<<endl;
}*/
if (dis[gx][gy] == inf)
cout << -1 << endl;
else
cout << dis[gx][gy] << endl;
}
| replace | 62 | 64 | 62 | 66 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};
int main() {
uint32_t H = 0, W = 0, K = 0;
std::cin >> H >> W >> K;
std::pair<uint32_t, uint32_t> start, goal;
std::cin >> start.first >> start.second >> goal.first >> goal.second;
std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
for (size_t i = 0; i < H; i++) {
std::string str;
std::cin >> str;
for (size_t j = 0; j < W; j++) {
if (str[j] == '.')
G[i + 1][j + 1] = 0;
}
}
std::queue<std::pair<uint32_t, uint32_t>> Q;
Q.push(start);
while (!Q.empty()) {
uint32_t x = Q.front().first, y = Q.front().second;
Q.pop();
for (size_t i = 0; i < dx.size(); i++) {
for (size_t k = 1; k <= K; k++) {
uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
if ((to_x == start.first && to_y == start.second) ||
G[to_x][to_y] == -1)
break;
if (G[to_x][to_y] != 0) {
if (G[to_x][to_y] < G[x][y])
break;
continue;
}
G[to_x][to_y] = G[x][y] + 1;
Q.push({to_x, to_y});
}
}
}
std::cout << (G[goal.first][goal.second] == 0 ? -1
: G[goal.first][goal.second])
<< std::endl;
return 0;
} | #include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};
int main() {
uint32_t H = 0, W = 0, K = 0;
std::cin >> H >> W >> K;
std::pair<uint32_t, uint32_t> start, goal;
std::cin >> start.first >> start.second >> goal.first >> goal.second;
std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
for (size_t i = 0; i < H; i++) {
std::string str;
std::cin >> str;
for (size_t j = 0; j < W; j++) {
if (str[j] == '.')
G[i + 1][j + 1] = 0;
}
}
std::queue<std::pair<uint32_t, uint32_t>> Q;
Q.push(start);
while (!Q.empty()) {
uint32_t x = Q.front().first, y = Q.front().second;
Q.pop();
for (size_t i = 0; i < dx.size(); i++) {
for (size_t k = 1; k <= K; k++) {
uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
if ((to_x == start.first && to_y == start.second) ||
G[to_x][to_y] == -1)
break;
if (G[to_x][to_y] != 0) {
if (G[to_x][to_y] <= G[x][y])
break;
continue;
}
G[to_x][to_y] = G[x][y] + 1;
Q.push({to_x, to_y});
}
}
}
std::cout << (G[goal.first][goal.second] == 0 ? -1
: G[goal.first][goal.second])
<< std::endl;
return 0;
} | replace | 37 | 38 | 37 | 38 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, a, b) for (ll i = a; i < b; i++)
#define pb emplace_back
#define allv v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
#define FASTio \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define deb(x) cout << (#x) << " " << x << endl
ll mod = 1e9 + 7;
template <typename t> ll Max(ll a, ll b) { return (a > b) ? a : b; }
ll Min(ll a, ll b) { return (a > b) ? b : a; }
ll power(ll a, ll b) {
ll p = 1;
a = a % mod;
b = b % mod;
while (b) {
if (b & 1) {
p = ((p % mod) * (a % mod)) % mod;
}
a = ((a % mod) * (a % mod)) % mod;
b = (b / 2);
}
return p % mod;
}
ll h, w, k;
ll ma = 1e15;
bool isValid(ll i, ll j) { return (i >= 1 && i <= h && j >= 1 && j <= w); }
ll direc[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
int t;
t = 1;
// cin>>t;
while (t--) {
// ll h,w,k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
char mat[h + 1][w + 1];
Rep(i, 1, h + 1) { Rep(j, 1, w + 1) cin >> mat[i][j]; }
ll dist[h + 1][w + 1];
bool visited[h + 1][w + 1];
// memset(dist,INT_MAX,sizeof(dist));
for (ll i = 1; i <= h; i++) {
for (ll j = 1; j <= w; j++) {
dist[i][j] = -1;
visited[i][j] = false;
}
}
dist[x1][y1] = 0;
visited[x1][y1] = true;
queue<pair<ll, ll>> q;
q.push(make_pair(x1, y1));
while (!q.empty()) {
pair<ll, ll> curr = q.front();
q.pop();
ll i = curr.first;
ll j = curr.second;
for (ll p = 0; p < 4; p++) {
for (ll Q = 1; Q <= k; Q++) {
ll i1 = i + direc[p][0] * Q;
ll j1 = j + direc[p][1] * Q;
if (isValid(i1, j1) && mat[i1][j1] != '@') {
if (dist[i1][j1] == -1) {
dist[i1][j1] = dist[i][j] + 1;
q.push(make_pair(i1, j1));
} else {
if (dist[i1][j1] >= (dist[i][j] + 1))
dist[i1][j1] = dist[i][j] + 1;
}
} else
break;
}
}
}
cout << dist[x2][y2] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, a, b) for (ll i = a; i < b; i++)
#define pb emplace_back
#define allv v.begin(), v.end()
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
#define FASTio \
ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define deb(x) cout << (#x) << " " << x << endl
ll mod = 1e9 + 7;
template <typename t> ll Max(ll a, ll b) { return (a > b) ? a : b; }
ll Min(ll a, ll b) { return (a > b) ? b : a; }
ll power(ll a, ll b) {
ll p = 1;
a = a % mod;
b = b % mod;
while (b) {
if (b & 1) {
p = ((p % mod) * (a % mod)) % mod;
}
a = ((a % mod) * (a % mod)) % mod;
b = (b / 2);
}
return p % mod;
}
ll h, w, k;
ll ma = 1e15;
bool isValid(ll i, ll j) { return (i >= 1 && i <= h && j >= 1 && j <= w); }
ll direc[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
int t;
t = 1;
// cin>>t;
while (t--) {
// ll h,w,k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
char mat[h + 1][w + 1];
Rep(i, 1, h + 1) { Rep(j, 1, w + 1) cin >> mat[i][j]; }
ll dist[h + 1][w + 1];
bool visited[h + 1][w + 1];
// memset(dist,INT_MAX,sizeof(dist));
for (ll i = 1; i <= h; i++) {
for (ll j = 1; j <= w; j++) {
dist[i][j] = -1;
visited[i][j] = false;
}
}
dist[x1][y1] = 0;
visited[x1][y1] = true;
queue<pair<ll, ll>> q;
q.push(make_pair(x1, y1));
while (!q.empty()) {
pair<ll, ll> curr = q.front();
q.pop();
ll i = curr.first;
ll j = curr.second;
for (ll p = 0; p < 4; p++) {
for (ll Q = 1; Q <= k; Q++) {
ll i1 = i + direc[p][0] * Q;
ll j1 = j + direc[p][1] * Q;
if (isValid(i1, j1) && mat[i1][j1] != '@') {
if (dist[i1][j1] == -1) {
dist[i1][j1] = dist[i][j] + 1;
q.push(make_pair(i1, j1));
} else {
if (dist[i1][j1] >= (dist[i][j] + 1))
dist[i1][j1] = dist[i][j] + 1;
else
break;
}
} else
break;
}
}
}
cout << dist[x2][y2] << endl;
}
return 0;
}
| insert | 74 | 74 | 74 | 76 | TLE | |
p02644 | C++ | Time Limit Exceeded |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() { cerr << endl; }
template <typename H, typename... T> void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T> void print(T x) { cout << x << endl; }
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;
ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<vector<char>> m(H, vector<char>(W));
REP(i, H) REP(j, W) cin >> m[i][j];
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
queue<pair<ll, Pin>> q;
q.push({0, {x1, y1}});
vector<vll> dist(H, vll(W, INF));
while (!q.empty()) {
auto p = q.front();
q.pop();
ll cost = p.fi;
int x = p.se.fi, y = p.se.se;
REP(i, 4) {
FOR(k, 1, K + 1) {
int nx = x + dx[i] * k, ny = y + dy[i] * k;
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (m[nx][ny] == '@')
break;
if (cost + 1 < dist[nx][ny]) {
q.push({cost + 1, {nx, ny}});
dist[nx][ny] = cost + 1;
}
}
}
}
print(dist[x2][y2] == INF ? -1 : dist[x2][y2]);
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() { cerr << endl; }
template <typename H, typename... T> void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T> void print(T x) { cout << x << endl; }
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;
ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<vector<char>> m(H, vector<char>(W));
REP(i, H) REP(j, W) cin >> m[i][j];
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
queue<pair<ll, Pin>> q;
q.push({0, {x1, y1}});
vector<vll> dist(H, vll(W, INF));
while (!q.empty()) {
auto p = q.front();
q.pop();
ll cost = p.fi;
int x = p.se.fi, y = p.se.se;
REP(i, 4) {
FOR(k, 1, K + 1) {
int nx = x + dx[i] * k, ny = y + dy[i] * k;
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
continue;
if (m[nx][ny] == '@')
break;
if (cost + 1 < dist[nx][ny]) {
q.push({cost + 1, {nx, ny}});
dist[nx][ny] = cost + 1;
}
if (dist[nx][ny] == cost + 1)
continue;
else
break;
}
}
}
print(dist[x2][y2] == INF ? -1 : dist[x2][y2]);
}
| insert | 84 | 84 | 84 | 88 | TLE | |
p02644 | C++ | Time Limit Exceeded | // KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)
#define endl '\n'
#define debug cout << "\n========================================\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl;
#define err4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl;
#define all(a) a.begin(), a.end()
#define show(a) \
for (auto xyz : a) \
cout << xyz << " "; \
cout << endl;
#define PQ priority_queue
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB \
lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
// OUR VALUE
#define UB \
upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
// OUR VALUE
#define fr first
#define sc second
#define MAXN 500000
#define INF 9223372036854775807
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, k;
bool in(int &i, int &j) { return (i >= 1 && j >= 1 && i <= h && j <= w); }
signed main() {
fastio cin >> h >> w >> k;
int si, sj, ei, ej;
cin >> si >> sj >> ei >> ej;
char a[h + 1][w + 1];
int d[h + 1][w + 1];
f(i, 1, h + 1) f(j, 1, w + 1) {
cin >> a[i][j];
d[i][j] = INF;
}
d[si][sj] = 0;
PQ<pair<int, pair<int, int>>> pq;
f(i, 1, h + 1) f(j, 1, w + 1) if (a[i][j] != '@')
pq.push({(-1) * d[i][j], {i, j}});
while (pq.size()) {
auto ft = pq.top();
pq.pop();
int i = ft.sc.fr, j = ft.sc.sc;
if (d[i][j] == INF) {
puts("-1");
return 0;
}
if (i == ei && j == ej) {
cout << d[ei][ej];
return 0;
}
f(dir, 0, 4) {
int ni = i + dx[dir], nj = j + dy[dir], cnt = 0;
while (in(ni, nj) && cnt < k && a[ni][nj] == '.') {
if (d[ni][nj] > 1 + d[i][j]) {
d[ni][nj] = 1 + d[i][j];
pq.push({(-1) * d[ni][nj], {ni, nj}});
}
ni += dx[dir], nj += dy[dir], cnt++;
}
}
}
} | // KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)
#define endl '\n'
#define debug cout << "\n========================================\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl;
#define err4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl;
#define all(a) a.begin(), a.end()
#define show(a) \
for (auto xyz : a) \
cout << xyz << " "; \
cout << endl;
#define PQ priority_queue
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB \
lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
// OUR VALUE
#define UB \
upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
// OUR VALUE
#define fr first
#define sc second
#define MAXN 500000
#define INF 9223372036854775807
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, k;
bool in(int &i, int &j) { return (i >= 1 && j >= 1 && i <= h && j <= w); }
signed main() {
fastio cin >> h >> w >> k;
int si, sj, ei, ej;
cin >> si >> sj >> ei >> ej;
char a[h + 1][w + 1];
int d[h + 1][w + 1];
f(i, 1, h + 1) f(j, 1, w + 1) {
cin >> a[i][j];
d[i][j] = INF;
}
d[si][sj] = 0;
PQ<pair<int, pair<int, int>>> pq;
f(i, 1, h + 1) f(j, 1, w + 1) if (a[i][j] != '@')
pq.push({(-1) * d[i][j], {i, j}});
while (pq.size()) {
auto ft = pq.top();
pq.pop();
int i = ft.sc.fr, j = ft.sc.sc;
if (d[i][j] == INF) {
puts("-1");
return 0;
}
if (i == ei && j == ej) {
cout << d[ei][ej];
return 0;
}
f(dir, 0, 4) {
int ni = i + dx[dir], nj = j + dy[dir], cnt = 0;
while (in(ni, nj) && cnt < k && a[ni][nj] == '.') {
if (d[ni][nj] > 1 + d[i][j]) {
d[ni][nj] = 1 + d[i][j];
pq.push({(-1) * d[ni][nj], {ni, nj}});
} else if (d[ni][nj] < 1 + d[i][j]) {
break;
}
ni += dx[dir], nj += dy[dir], cnt++;
}
}
}
} | insert | 93 | 93 | 93 | 95 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define fori(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define int long long
#define F first
#define S second
#define mp make_pair
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
cout.precision(15);
int mod = 1e9 + 7;
int pow(int a, int b) {
int ans = 1;
a %= mod;
while (b) {
if (b % 2) {
ans *= a;
ans %= mod;
}
b /= 2;
a *= a;
a %= mod;
}
return ans;
}
int inv(int a) { return pow(a, mod - 2); }
int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
struct cmp {
bool operator()(pair<int, int> a, pair<int, int> b) const {
int x = a.F;
int y = b.F;
return x >= y;
}
};
int h, k, w, x, y, u, v;
vector<string> mat;
vector<vector<int>> vis;
bool valid(int x, int y) { return (x >= 0 && y >= 0 && x < h && y < w); }
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void bfs() {
queue<pair<int, int>> q;
vis[x][y] = 1;
q.push({x, y});
while (q.size()) {
auto cur = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
for (int i = 1; i <= k; i++) {
int nx = cur.F + dx[d] * i;
int ny = cur.S + dy[d] * i;
if (!valid(nx, ny) || mat[nx][ny] == '@')
break;
if (vis[nx][ny])
continue;
vis[nx][ny] = vis[cur.F][cur.S] + 1;
q.push({nx, ny});
}
}
}
}
int32_t main() {
FAST;
int t;
// cin>>t;
t = 1;
while (t--) {
cin >> h >> w >> k >> x >> y >> u >> v;
x--, y--, u--, v--;
vis.resize(h);
fori(i, h) vis[i].resize(w);
fori(i, h) fori(j, w) vis[i][j] = 0;
string s;
fori(i, h) cin >> s, mat.pb(s);
bfs();
if (!vis[u][v])
cout << "-1";
else
cout << vis[u][v] - 1;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fori(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define ll long long
#define int long long
#define F first
#define S second
#define mp make_pair
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
cout.precision(15);
int mod = 1e9 + 7;
int pow(int a, int b) {
int ans = 1;
a %= mod;
while (b) {
if (b % 2) {
ans *= a;
ans %= mod;
}
b /= 2;
a *= a;
a %= mod;
}
return ans;
}
int inv(int a) { return pow(a, mod - 2); }
int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
struct cmp {
bool operator()(pair<int, int> a, pair<int, int> b) const {
int x = a.F;
int y = b.F;
return x >= y;
}
};
int h, k, w, x, y, u, v;
vector<string> mat;
vector<vector<int>> vis;
bool valid(int x, int y) { return (x >= 0 && y >= 0 && x < h && y < w); }
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void bfs() {
queue<pair<int, int>> q;
vis[x][y] = 1;
q.push({x, y});
while (q.size()) {
auto cur = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
for (int i = 1; i <= k; i++) {
int nx = cur.F + dx[d] * i;
int ny = cur.S + dy[d] * i;
if (!valid(nx, ny) || mat[nx][ny] == '@')
break;
if (vis[nx][ny] && vis[nx][ny] < 1 + vis[cur.F][cur.S])
break;
if (vis[nx][ny])
continue;
vis[nx][ny] = vis[cur.F][cur.S] + 1;
q.push({nx, ny});
}
}
}
}
int32_t main() {
FAST;
int t;
// cin>>t;
t = 1;
while (t--) {
cin >> h >> w >> k >> x >> y >> u >> v;
x--, y--, u--, v--;
vis.resize(h);
fori(i, h) vis[i].resize(w);
fori(i, h) fori(j, w) vis[i][j] = 0;
string s;
fori(i, h) cin >> s, mat.pb(s);
bfs();
if (!vis[u][v])
cout << "-1";
else
cout << vis[u][v] - 1;
}
return 0;
} | insert | 69 | 69 | 69 | 71 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template <class T> inline bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T> inline bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W, K, sx, sy, tx, ty;
cin >> H >> W >> K >> sx >> sy >> tx >> ty;
--sx, --sy, --tx, --ty;
vector<string> s(H);
for (int i = 0; i < H; ++i)
cin >> s[i];
vector<vector<int>> dist(H, vector<int>(W, INF));
using Data = tuple<int, int, int>;
priority_queue<Data, vector<Data>, greater<Data>> que;
dist[sx][sy] = 0;
que.emplace(0, sx, sy);
while (!que.empty()) {
int d, x, y;
tie(d, x, y) = que.top();
que.pop();
if (d > dist[x][y])
continue;
for (int i = 0; i < 4; ++i) {
int nx = x, ny = y;
for (int j = 0; j < K; ++j) {
nx += dx[i], ny += dy[i];
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
break;
if (s[nx][ny] == '@')
break;
if (chmin(dist[nx][ny], dist[x][y] + 1))
que.emplace(dist[nx][ny], nx, ny);
}
}
}
if (dist[tx][ty] == INF)
dist[tx][ty] = -1;
cout << dist[tx][ty] << endl;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template <class T> inline bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T> inline bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W, K, sx, sy, tx, ty;
cin >> H >> W >> K >> sx >> sy >> tx >> ty;
--sx, --sy, --tx, --ty;
vector<string> s(H);
for (int i = 0; i < H; ++i)
cin >> s[i];
vector<vector<int>> dist(H, vector<int>(W, INF));
using Data = tuple<int, int, int>;
priority_queue<Data, vector<Data>, greater<Data>> que;
dist[sx][sy] = 0;
que.emplace(0, sx, sy);
while (!que.empty()) {
int d, x, y;
tie(d, x, y) = que.top();
que.pop();
if (d > dist[x][y])
continue;
for (int i = 0; i < 4; ++i) {
int nx = x, ny = y;
for (int j = 0; j < K; ++j) {
nx += dx[i], ny += dy[i];
if (nx < 0 || nx >= H || ny < 0 || ny >= W)
break;
if (s[nx][ny] == '@')
break;
if (d + 1 > dist[nx][ny])
break;
if (chmin(dist[nx][ny], d + 1))
que.emplace(dist[nx][ny], nx, ny);
}
}
}
if (dist[tx][ty] == INF)
dist[tx][ty] = -1;
cout << dist[tx][ty] << endl;
} | replace | 79 | 80 | 79 | 82 | TLE | |
p02644 | C++ | Time Limit Exceeded | // Coded by Abhijay Mitra
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
// #include <bits/stdc++.h>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb push_back
#define res(v) v.resize(unique(v.begin(), v.end()) - v.begin());
#define cnt_res(v) std::distance(v.begin(), std::unique(v.begin(), v.end()));
#define timer \
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e6 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
vvi dp, vis, visa;
vector<string> s;
int X1, Y1, X2, Y2, K, ans = inf;
int h, w;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(int i, int j) {
queue<pair<pii, int>> q;
q.push({{i, j}, 0});
while (q.empty() == 0) {
auto z = q.front();
q.pop();
if (z.F == mp(X2, Y2)) {
ans = min(ans, z.S);
break;
}
vis[z.F.F][z.F.S] = 1;
visa[z.F.F][z.F.S] = 1;
rep(k, 0, 3) rep(I, 1, K) {
int x = z.F.F + I * dx[k], y = z.F.S + I * dy[k];
if (x < h and x >= 0 and y < w and y >= 0 and s[x][y] == '.') {
if (vis[x][y] == 0)
q.push({{x, y}, z.S + 1});
}
if (x < h and x >= 0 and y < w and y >= 0 and s[x][y] == '@')
break;
if (x < h and x >= 0 and y < w and y >= 0 and visa[x][y] == 1)
break;
if (x >= h or x < 0 or y < 0 or y >= w)
break;
}
}
}
void solve() {
cin >> h >> w >> K;
s = vector<string>(h);
dp = vis = visa = vvi(h, vi(w, 0));
cin >> X1 >> Y1 >> X2 >> Y2;
rep(i, 0, h - 1) cin >> s[i];
X1--, Y1--, X2--, Y2--;
bfs(X1, Y1);
if (ans == inf)
cout << -1;
else
cout << ans;
}
int32_t main() {
ibs;
cti;
solve()
/*,cout<<"\n"*/;
// cout<<"\n";
int xx = 0;
// int t;cin>>t;while(t--){/*xx++;cout<<"Case
// "<<xx<<":\n"*/;solve();cout<<"\n";}
return 0;
} | // Coded by Abhijay Mitra
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
// #include <bits/stdc++.h>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb push_back
#define res(v) v.resize(unique(v.begin(), v.end()) - v.begin());
#define cnt_res(v) std::distance(v.begin(), std::unique(v.begin(), v.end()));
#define timer \
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e6 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
vvi dp, vis, visa;
vector<string> s;
int X1, Y1, X2, Y2, K, ans = inf;
int h, w;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void bfs(int i, int j) {
queue<pair<pii, int>> q;
q.push({{i, j}, 0});
while (q.empty() == 0) {
auto z = q.front();
q.pop();
if (z.F == mp(X2, Y2)) {
ans = min(ans, z.S);
break;
}
vis[z.F.F][z.F.S] = 1;
visa[z.F.F][z.F.S] = 1;
rep(k, 0, 3) rep(I, 1, K) {
int x = z.F.F + I * dx[k], y = z.F.S + I * dy[k];
if (x < h and x >= 0 and y < w and y >= 0 and s[x][y] == '.') {
if (vis[x][y] == 0)
q.push({{x, y}, z.S + 1}), vis[x][y] = 1;
}
if (x < h and x >= 0 and y < w and y >= 0 and s[x][y] == '@')
break;
if (x < h and x >= 0 and y < w and y >= 0 and visa[x][y] == 1)
break;
if (x >= h or x < 0 or y < 0 or y >= w)
break;
}
}
}
void solve() {
cin >> h >> w >> K;
s = vector<string>(h);
dp = vis = visa = vvi(h, vi(w, 0));
cin >> X1 >> Y1 >> X2 >> Y2;
rep(i, 0, h - 1) cin >> s[i];
X1--, Y1--, X2--, Y2--;
bfs(X1, Y1);
if (ans == inf)
cout << -1;
else
cout << ans;
}
int32_t main() {
ibs;
cti;
solve()
/*,cout<<"\n"*/;
// cout<<"\n";
int xx = 0;
// int t;cin>>t;while(t--){/*xx++;cout<<"Case
// "<<xx<<":\n"*/;solve();cout<<"\n";}
return 0;
} | replace | 67 | 68 | 67 | 68 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;
#define eb emplace_back
#define pb push_back
#define MP make_pair
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define rep(i, j, n) for (int i = j; i < n; ++i)
#define repn(i, j, n) for (int i = j; i <= n; ++i)
#define revn(i, j, n) for (int i = j; i >= n; --i)
#define sz(i) i.size()
#define mem(i, v) memset(i, v, sizeof(i))
#define all(v) v.begin(), v.end()
#define endl '\n'
// #define int long long
#define INF 1e9
#define ss second
#define ff first
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pair<int, int>> piii;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool f = 1;
string r = "{";
for (const auto &x : v) {
if (!f)
r += ", ";
f = 0;
r += to_string(x);
}
return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
const int mod = 1e9 + 7;
// const int mod=998244353;
const int up = 1e5 + 10;
const int MAXN = 1e6 + 10;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void solve() {
int h, w, k, x1, x2, y1, y2, x, y, nx, ny;
cin >> h >> w >> k;
char a[h + 1][w + 1];
int dis[h + 1][w + 1], vis[h + 1][w + 1];
string s;
cin >> x1 >> y1 >> x2 >> y2;
repn(i, 1, h) {
cin >> s;
repn(j, 1, w) {
a[i][j] = s[j - 1];
dis[i][j] = INF;
vis[i][j] = 0;
}
}
dis[x1][y1] = 0;
queue<pii> q;
q.push({x1, y1});
while (!q.empty()) {
x = q.front().ff;
y = q.front().ss;
q.pop();
vis[x][y] = 1;
// cout<<x<<" "<<y<<endl;
// if(x==x2 && y==y2)
// {
// cout<<"HAHA"<<endl;
// }
repn(i, 0, 3) {
repn(l, 1, k) {
nx = x + l * dx[i];
ny = y + l * dy[i];
if (nx >= 1 && nx <= h && ny >= 1 && ny <= w) {
if (a[nx][ny] == '@')
break;
if (vis[nx][ny])
break;
if (dis[nx][ny] != INF) {
continue;
}
if (dis[nx][ny] > dis[x][y] + 1) {
dis[nx][ny] = dis[x][y] + 1;
q.push({nx, ny});
}
}
}
}
}
if (dis[x2][y2] == INF) {
cout << -1 << endl;
return;
}
cout << dis[x2][y2] << endl;
}
int32_t main() {
fast_cin();
int T;
// sieve();
// cin>>T;
T = 1;
int t1 = 1;
while (true) {
// cout<<"Case "<<t1<<": ";
solve();
t1++;
if (t1 > T)
break;
}
// cout<<ans<<endl;
}
| #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
using T = tuple<int, int, int>;
#define eb emplace_back
#define pb push_back
#define MP make_pair
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define rep(i, j, n) for (int i = j; i < n; ++i)
#define repn(i, j, n) for (int i = j; i <= n; ++i)
#define revn(i, j, n) for (int i = j; i >= n; --i)
#define sz(i) i.size()
#define mem(i, v) memset(i, v, sizeof(i))
#define all(v) v.begin(), v.end()
#define endl '\n'
// #define int long long
#define INF 1e9
#define ss second
#define ff first
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pair<int, int>> piii;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
bool f = 1;
string r = "{";
for (const auto &x : v) {
if (!f)
r += ", ";
f = 0;
r += to_string(x);
}
return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
const int mod = 1e9 + 7;
// const int mod=998244353;
const int up = 1e5 + 10;
const int MAXN = 1e6 + 10;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void solve() {
int h, w, k, x1, x2, y1, y2, x, y, nx, ny;
cin >> h >> w >> k;
char a[h + 1][w + 1];
int dis[h + 1][w + 1], vis[h + 1][w + 1];
string s;
cin >> x1 >> y1 >> x2 >> y2;
repn(i, 1, h) {
cin >> s;
repn(j, 1, w) {
a[i][j] = s[j - 1];
dis[i][j] = INF;
vis[i][j] = 0;
}
}
dis[x1][y1] = 0;
queue<pii> q;
q.push({x1, y1});
while (!q.empty()) {
x = q.front().ff;
y = q.front().ss;
q.pop();
vis[x][y] = 1;
// cout<<x<<" "<<y<<endl;
// if(x==x2 && y==y2)
// {
// cout<<"HAHA"<<endl;
// }
repn(i, 0, 3) {
repn(l, 1, k) {
nx = x + l * dx[i];
ny = y + l * dy[i];
if (nx >= 1 && nx <= h && ny >= 1 && ny <= w) {
if (a[nx][ny] == '@')
break;
if (vis[nx][ny])
break;
if (dis[nx][ny] != INF && dis[x][y] + 1 > dis[nx][ny]) {
break;
} else if (dis[nx][ny] != INF) {
continue;
}
if (dis[nx][ny] > dis[x][y] + 1) {
dis[nx][ny] = dis[x][y] + 1;
q.push({nx, ny});
}
}
}
}
}
if (dis[x2][y2] == INF) {
cout << -1 << endl;
return;
}
cout << dis[x2][y2] << endl;
}
int32_t main() {
fast_cin();
int T;
// sieve();
// cin>>T;
T = 1;
int t1 = 1;
while (true) {
// cout<<"Case "<<t1<<": ";
solve();
t1++;
if (t1 > T)
break;
}
// cout<<ans<<endl;
}
| replace | 115 | 116 | 115 | 118 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<int, 2> pii;
typedef vector<int> vi;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int n, m, k;
bool ok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<char>> c(n + 2, vector<char>(m + 2));
vector<vi> u(n + 2, vi(m + 2));
vector<vi> d(n + 2, vi(m + 2));
vector<vi> l(n + 2, vi(m + 2));
vector<vi> r(n + 2, vi(m + 2));
vector<vi> dist(n + 2, vi(m + 2));
vector<vi> used(n + 2, vi(m + 2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
if (i == n) {
u[i][j] = -1;
} else {
u[i][j] = i + 1;
}
if (i == 1) {
d[i][j] = -1;
} else {
d[i][j] = i - 1;
}
if (j == m) {
r[i][j] = -1;
} else {
r[i][j] = j + 1;
}
if (j == 1) {
l[i][j] = -1;
} else {
l[i][j] = j - 1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] != '.') {
int X = i, Y = j;
if (u[X][Y] != -1)
d[u[X][Y]][Y] = -1;
if (d[X][Y] != -1)
u[d[X][Y]][Y] = -1;
if (r[X][Y] != -1)
l[X][r[X][Y]] = -1;
if (l[X][Y] != -1)
r[X][l[X][Y]] = -1;
}
}
}
queue<pii> q;
q.push({x1, y1});
used[x1][y1] = 1;
dist[x1][y1] = 0;
if (u[x1][y1] != -1)
d[u[x1][y1]][y1] = d[x1][y1];
if (d[x1][y1] != -1)
u[d[x1][y1]][y1] = u[x1][y1];
if (r[x1][y1] != -1)
l[x1][r[x1][y1]] = l[x1][y1];
if (l[x1][y1] != -1)
r[x1][l[x1][y1]] = r[x1][y1];
pii tmp;
while (!q.empty()) {
pii v = q.front();
q.pop();
int x = v[0], y = v[1], ind = 0;
int X = x, Y = y;
while (ind < 4) {
int X0 = X, Y0 = Y;
if (ind == 0) {
X = d[X][Y];
}
if (ind == 1) {
X = u[X][Y];
}
if (ind == 2) {
Y = l[X][Y];
}
if (ind == 3) {
Y = r[X][Y];
}
if (!ok(X, Y) || abs(X - x) > k || abs(Y - y) > k) {
X = x, Y = y;
ind++;
continue;
}
// if (ind == 0) {
// d[X0][Y0] = -1;
//}
// if (ind == 1) {
// u[X0][Y0] = -1;
//}
// if (ind == 2) {
// l[X0][Y0] = -1;
//}
// if (ind == 3) {
// r[X0][Y0] = -1;
//}
if (used[X][Y]) {
continue;
}
dist[X][Y] = dist[x][y] + 1;
used[X][Y] = 1;
tmp[0] = X, tmp[1] = Y;
q.push(tmp);
if (u[X][Y] != -1)
d[u[X][Y]][Y] = d[X][Y];
if (d[X][Y] != -1)
u[d[X][Y]][Y] = u[X][Y];
if (r[X][Y] != -1)
l[X][r[X][Y]] = l[X][Y];
if (l[X][Y] != -1)
r[X][l[X][Y]] = r[X][Y];
}
}
if (!used[x2][y2]) {
cout << -1;
} else {
cout << dist[x2][y2];
}
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<int, 2> pii;
typedef vector<int> vi;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int n, m, k;
bool ok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<char>> c(n + 2, vector<char>(m + 2));
vector<vi> u(n + 2, vi(m + 2));
vector<vi> d(n + 2, vi(m + 2));
vector<vi> l(n + 2, vi(m + 2));
vector<vi> r(n + 2, vi(m + 2));
vector<vi> dist(n + 2, vi(m + 2));
vector<vi> used(n + 2, vi(m + 2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
if (i == n) {
u[i][j] = -1;
} else {
u[i][j] = i + 1;
}
if (i == 1) {
d[i][j] = -1;
} else {
d[i][j] = i - 1;
}
if (j == m) {
r[i][j] = -1;
} else {
r[i][j] = j + 1;
}
if (j == 1) {
l[i][j] = -1;
} else {
l[i][j] = j - 1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (c[i][j] != '.') {
int X = i, Y = j;
if (u[X][Y] != -1)
d[u[X][Y]][Y] = -1;
if (d[X][Y] != -1)
u[d[X][Y]][Y] = -1;
if (r[X][Y] != -1)
l[X][r[X][Y]] = -1;
if (l[X][Y] != -1)
r[X][l[X][Y]] = -1;
}
}
}
queue<pii> q;
q.push({x1, y1});
used[x1][y1] = 1;
dist[x1][y1] = 0;
if (u[x1][y1] != -1)
d[u[x1][y1]][y1] = d[x1][y1];
if (d[x1][y1] != -1)
u[d[x1][y1]][y1] = u[x1][y1];
if (r[x1][y1] != -1)
l[x1][r[x1][y1]] = l[x1][y1];
if (l[x1][y1] != -1)
r[x1][l[x1][y1]] = r[x1][y1];
pii tmp;
while (!q.empty()) {
pii v = q.front();
q.pop();
int x = v[0], y = v[1], ind = 0;
int X = x, Y = y;
while (ind < 4) {
int X0 = X, Y0 = Y;
if (ind == 0) {
X = d[X][Y];
}
if (ind == 1) {
X = u[X][Y];
}
if (ind == 2) {
Y = l[X][Y];
}
if (ind == 3) {
Y = r[X][Y];
}
if (!ok(X, Y) || abs(X - x) > k || abs(Y - y) > k) {
X = x, Y = y;
ind++;
continue;
}
if (ind == 0) {
d[X0][Y0] = d[X][Y];
}
if (ind == 1) {
u[X0][Y0] = u[X][Y];
}
if (ind == 2) {
l[X0][Y0] = l[X][Y];
}
if (ind == 3) {
r[X0][Y0] = r[X][Y];
}
if (used[X][Y]) {
continue;
}
dist[X][Y] = dist[x][y] + 1;
used[X][Y] = 1;
tmp[0] = X, tmp[1] = Y;
q.push(tmp);
if (u[X][Y] != -1)
d[u[X][Y]][Y] = d[X][Y];
if (d[X][Y] != -1)
u[d[X][Y]][Y] = u[X][Y];
if (r[X][Y] != -1)
l[X][r[X][Y]] = l[X][Y];
if (l[X][Y] != -1)
r[X][l[X][Y]] = r[X][Y];
}
}
if (!used[x2][y2]) {
cout << -1;
} else {
cout << dist[x2][y2];
}
}
| replace | 121 | 133 | 121 | 133 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a1, a2, b1, b2;
char c[1000010];
char s[1000010];
int dis[1000010];
struct node {
int x, y;
};
queue<node> q;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int f(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
if (x < 1 || x > n)
return false;
if (y < 1 || y > m)
return false;
if (c[(x - 1) * m + y] == '@')
return false;
return true;
}
int main() {
memset(dis, 0x3f, sizeof(dis));
cin >> n >> m >> k;
cin >> a1 >> b1 >> a2 >> b2;
for (register int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (register int j = 1; j <= m; ++j) {
c[(i - 1) * m + j] = s[j];
}
}
q.push(node{a1, b1});
dis[(a1 - 1) * m + b1] = 0;
while (!q.empty()) {
node now = q.front();
q.pop();
int num = dis[f(now.x, now.y)] + 1;
for (register int i = 0; i < 4; ++i) {
int xx = now.x, yy = now.y;
for (register int j = 1; j <= k; ++j) {
xx += dx[i];
yy += dy[i];
if (!check(xx, yy))
break;
if (dis[f(xx, yy)] > num) {
dis[f(xx, yy)] = num;
q.push(node{xx, yy});
}
}
}
}
if (dis[f(a2, b2)] < 0x3f3f3f3f)
cout << dis[f(a2, b2)];
else
puts("-1");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a1, a2, b1, b2;
char c[1000010];
char s[1000010];
int dis[1000010];
struct node {
int x, y;
};
queue<node> q;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int f(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
if (x < 1 || x > n)
return false;
if (y < 1 || y > m)
return false;
if (c[(x - 1) * m + y] == '@')
return false;
return true;
}
int main() {
memset(dis, 0x3f, sizeof(dis));
cin >> n >> m >> k;
cin >> a1 >> b1 >> a2 >> b2;
for (register int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (register int j = 1; j <= m; ++j) {
c[(i - 1) * m + j] = s[j];
}
}
q.push(node{a1, b1});
dis[(a1 - 1) * m + b1] = 0;
while (!q.empty()) {
node now = q.front();
q.pop();
int num = dis[f(now.x, now.y)] + 1;
for (register int i = 0; i < 4; ++i) {
int xx = now.x, yy = now.y;
for (register int j = 1; j <= k; ++j) {
xx += dx[i];
yy += dy[i];
if (!check(xx, yy) || dis[f(xx, yy)] < dis[f(now.x, now.y)] + 1)
break;
if (dis[f(xx, yy)] > num) {
dis[f(xx, yy)] = num;
q.push(node{xx, yy});
}
}
}
}
if (dis[f(a2, b2)] < 0x3f3f3f3f)
cout << dis[f(a2, b2)];
else
puts("-1");
return 0;
} | replace | 44 | 45 | 44 | 45 | TLE | |
p02644 | C++ | Time Limit Exceeded | #ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define EPS (1e-7)
#define IINF ((int)1e9)
MY_NAMESPACE(testbed)
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<vector<char>> pond(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> pond[i][j];
}
}
vector<vector<vector<int>>> min_step(
h, vector<vector<int>>(w, vector<int>(4, IINF)));
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
typedef tuple<int, int, int, int> T4int; // {step, x, y, direction(0-3)}
priority_queue<T4int, vector<T4int>, less<T4int>> q;
for (int i = 0; i <= 3; i++) {
min_step[x1][y1][i] = 0;
q.emplace(0, x1, y1, i);
}
while (!q.empty()) {
int this_step, this_x, this_y, this_dir;
tie(this_step, this_x, this_y, this_dir) = q.top();
q.pop();
// change the direction
for (int i = 0; i <= 3; i++) {
int next_step = ((this_step + k - 1) / k) * k;
if (min_step[this_x][this_y][i] > next_step) {
min_step[this_x][this_y][i] = next_step;
q.emplace(next_step, this_x, this_y, i);
}
}
// one step along with the direction
int next_x = this_x + dx[this_dir];
int next_y = this_y + dy[this_dir];
int next_dir = this_dir;
if (next_x < 0 || h <= next_x || next_y < 0 || w <= next_y)
continue;
if (min_step[next_x][next_y][next_dir] <= this_step)
continue;
if (pond[next_x][next_y] == '@')
continue;
min_step[next_x][next_y][next_dir] = this_step + 1;
q.emplace(this_step + 1, next_x, next_y, next_dir);
}
int res = IINF;
for (int i = 0; i <= 3; i++) {
if (min_step[x2][y2][i] == IINF)
continue;
int actual_step = (min_step[x2][y2][i] + k - 1) / k;
res = min(res, actual_step);
}
if (res == IINF)
res = -1;
cout << res << "\n";
return 0;
}
MY_NAMESPACE_ | #ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define EPS (1e-7)
#define IINF ((int)1e9)
MY_NAMESPACE(testbed)
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<vector<char>> pond(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> pond[i][j];
}
}
vector<vector<vector<int>>> min_step(
h, vector<vector<int>>(w, vector<int>(4, IINF)));
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
typedef tuple<int, int, int, int> T4int; // {step, x, y, direction(0-3)}
priority_queue<T4int, vector<T4int>, greater<T4int>> q;
for (int i = 0; i <= 3; i++) {
min_step[x1][y1][i] = 0;
q.emplace(0, x1, y1, i);
}
while (!q.empty()) {
int this_step, this_x, this_y, this_dir;
tie(this_step, this_x, this_y, this_dir) = q.top();
q.pop();
// change the direction
for (int i = 0; i <= 3; i++) {
int next_step = ((this_step + k - 1) / k) * k;
if (min_step[this_x][this_y][i] > next_step) {
min_step[this_x][this_y][i] = next_step;
q.emplace(next_step, this_x, this_y, i);
}
}
// one step along with the direction
int next_x = this_x + dx[this_dir];
int next_y = this_y + dy[this_dir];
int next_dir = this_dir;
if (next_x < 0 || h <= next_x || next_y < 0 || w <= next_y)
continue;
if (min_step[next_x][next_y][next_dir] <= this_step)
continue;
if (pond[next_x][next_y] == '@')
continue;
min_step[next_x][next_y][next_dir] = this_step + 1;
q.emplace(this_step + 1, next_x, next_y, next_dir);
}
int res = IINF;
for (int i = 0; i <= 3; i++) {
if (min_step[x2][y2][i] == IINF)
continue;
int actual_step = (min_step[x2][y2][i] + k - 1) / k;
res = min(res, actual_step);
}
if (res == IINF)
res = -1;
cout << res << "\n";
return 0;
}
MY_NAMESPACE_ | replace | 60 | 61 | 60 | 61 | TLE | |
p02644 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define owo \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define MOD (ll)(1e6 + 3)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs\n", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
vector<string> grid(h);
VII d(h + 1, vector<ll>(w + 1, INF));
for (int i = 0; i < h; i++)
cin >> grid[i];
auto ok = [&](ll x, ll y) {
return (x >= 0 && y >= 0 && x < h && y < w && grid[x][y] == '.');
};
queue<pii> q;
d[x1][y1] = 0;
q.push({x1, y1});
while (!q.empty()) {
ll x, y;
x = q.front().fi;
y = q.front().se;
q.pop();
if (x == x2 && y == y2) {
cout << d[x2][y2];
return 0;
}
for (int dir = 0; dir < 4; dir++) {
ll xn = x;
ll yn = y;
for (int i = 0; i < k; i++) {
xn += dx[dir];
yn += dy[dir];
if (!ok(xn, yn))
break;
if (d[xn][yn] > d[x][y] + 1) {
d[xn][yn] = d[x][y] + 1;
q.push({xn, yn});
}
}
}
// cout<<x<<" "<<y<<endl;
}
cout << -1;
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define owo \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define MOD (ll)(1e6 + 3)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for (long blockTime = 0; \
(blockTime == 0 ? (blockTime = clock()) != 0 : false); \
debug("%s time : %.4fs\n", d, \
(double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
vector<string> grid(h);
VII d(h + 1, vector<ll>(w + 1, INF));
for (int i = 0; i < h; i++)
cin >> grid[i];
auto ok = [&](ll x, ll y) {
return (x >= 0 && y >= 0 && x < h && y < w && grid[x][y] == '.');
};
queue<pii> q;
d[x1][y1] = 0;
q.push({x1, y1});
while (!q.empty()) {
ll x, y;
x = q.front().fi;
y = q.front().se;
q.pop();
if (x == x2 && y == y2) {
cout << d[x2][y2];
return 0;
}
for (int dir = 0; dir < 4; dir++) {
ll xn = x;
ll yn = y;
for (int i = 0; i < k; i++) {
xn += dx[dir];
yn += dy[dir];
if (!ok(xn, yn))
break;
if (d[xn][yn] <= d[x][y])
break;
if (d[xn][yn] > d[x][y] + 1) {
d[xn][yn] = d[x][y] + 1;
q.push({xn, yn});
}
}
}
// cout<<x<<" "<<y<<endl;
}
cout << -1;
}
| insert | 62 | 62 | 62 | 64 | TLE | |
p02644 | C++ | Runtime Error | #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <typename T>
std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) {
std::vector<T> tmp(s2, val);
return std::vector<std::vector<T>>(s1, tmp);
}
template <typename T> std::vector<T> mk1d(size_t s, T const &val) {
return std::vector<T>(s, val);
}
int const dx[4] = {1, 0, -1, 0};
int const dy[4] = {0, 1, 0, -1};
int64_t const INF = 1e18;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int H, W, K;
std::cin >> H >> W >> K;
int xs, ys, xg, yg;
std::cin >> xs >> ys >> xg >> yg;
auto bm = mk2d(H + 2, W + 2, '@');
for (int h = 1; h <= H; ++h)
for (int w = 1; w <= W; ++w)
std::cin >> bm[h][w];
auto dtmp = mk2d(H + 2, W + 2, INF);
auto dist = mk1d(4, dtmp);
using TP = std::tuple<int64_t, int, int, int>;
std::priority_queue<TP, std::vector<TP>, std::greater<TP>> q;
q.emplace(0, xs, ys, 0);
q.emplace(1, xs, ys, 0);
q.emplace(2, xs, ys, 0);
q.emplace(3, xs, ys, 0);
while (!q.empty()) {
int d, x, y;
int64_t c;
std::tie(c, d, x, y) = q.top();
q.pop();
if (c >= dist[d][x][y])
continue;
dist[d][x][y] = c;
for (int j = 1; j <= 3; ++j) {
int64_t nc = ((c + K - 1) / K) * K;
q.emplace(nc, (d + j) % 4, x, y);
}
int nx = x + dx[d];
int ny = y + dy[d];
if (bm[nx][ny] == '.')
q.emplace(c + 1, d, nx, ny);
}
int64_t ans = dist[0][xg][yg];
if (ans == INF)
ans = -1;
else
ans = (ans + K - 1) / K;
fin(ans);
return 0;
}
| #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <typename T>
std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) {
std::vector<T> tmp(s2, val);
return std::vector<std::vector<T>>(s1, tmp);
}
template <typename T> std::vector<T> mk1d(size_t s, T const &val) {
return std::vector<T>(s, val);
}
int const dx[4] = {1, 0, -1, 0};
int const dy[4] = {0, 1, 0, -1};
int64_t const INF = 1e18;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int H, W, K;
std::cin >> H >> W >> K;
int xs, ys, xg, yg;
std::cin >> xs >> ys >> xg >> yg;
auto bm = mk2d(H + 2, W + 2, '@');
for (int h = 1; h <= H; ++h)
for (int w = 1; w <= W; ++w)
std::cin >> bm[h][w];
auto dtmp = mk2d(H + 2, W + 2, INF);
auto dist = mk1d(4, dtmp);
using TP = std::tuple<int64_t, int, int, int>;
std::priority_queue<TP, std::vector<TP>, std::greater<TP>> q;
q.emplace(0, 0, xs, ys);
q.emplace(0, 1, xs, ys);
q.emplace(0, 2, xs, ys);
q.emplace(0, 3, xs, ys);
while (!q.empty()) {
int d, x, y;
int64_t c;
std::tie(c, d, x, y) = q.top();
q.pop();
if (c >= dist[d][x][y])
continue;
dist[d][x][y] = c;
for (int j = 1; j <= 3; ++j) {
int64_t nc = ((c + K - 1) / K) * K;
q.emplace(nc, (d + j) % 4, x, y);
}
int nx = x + dx[d];
int ny = y + dy[d];
if (bm[nx][ny] == '.')
q.emplace(c + 1, d, nx, ny);
}
int64_t ans = dist[0][xg][yg];
if (ans == INF)
ans = -1;
else
ans = (ans + K - 1) / K;
fin(ans);
return 0;
}
| replace | 40 | 44 | 40 | 44 | 0 | |
p02644 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int INF = 1e9 + 239;
const int N = 1e2 + 7;
const vector<int> dx = {0, 0, 1, -1};
const vector<int> dy = {1, -1, 0, 0};
int d[4 * N];
vector<int> g[4 * N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, K;
cin >> n >> m >> K;
int i1, i2, j1, j2;
cin >> i1 >> j1 >> i2 >> j2;
i1--;
i2--;
j1--;
j2--;
vector<vector<int>> ban(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
ban[i][j] = (c == '@');
}
}
fill(d, d + 4 * n * m, INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
int ni = i + dx[k];
int nj = j + dy[k];
if (0 <= ni && ni < n && 0 <= nj && nj < m && !ban[i][j] &&
!ban[ni][nj]) {
g[i * m + j + k * n * m].push_back(ni * m + nj + k * n * m);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!ban[i][j]) {
for (int k = 0; k < 4; k++) {
g[i * m + j + k * n * m].push_back(i * m + j + ((k + 1) % 4) * n * m);
}
}
}
}
d[i1 * m + j1] = 0;
set<pair<int, int>> s;
s.insert({d[i1 * m + j1], i1 * m + j1});
while (!s.empty()) {
int v = s.begin()->second;
s.erase(s.begin());
// cerr << d[v] << endl;
for (auto t : g[v]) {
if (abs(t - v) % (n * m)) {
if (d[t] > d[v] + 1) {
s.erase({d[t], t});
d[t] = d[v] + 1;
s.insert({d[t], t});
}
} else {
if (d[t] > ((d[v] + K - 1) / K) * K) {
s.erase({d[t], t});
d[t] = ((d[v] + K - 1) / K) * K;
s.insert({d[t], t});
}
}
}
}
int ans = d[i2 * m + j2];
if (ans == INF) {
cout << -1 << endl;
} else {
cout << (ans + K - 1) / K << endl;
}
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
const int INF = 1e9 + 239;
const int N = 1e6 + 7;
const vector<int> dx = {0, 0, 1, -1};
const vector<int> dy = {1, -1, 0, 0};
int d[4 * N];
vector<int> g[4 * N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, K;
cin >> n >> m >> K;
int i1, i2, j1, j2;
cin >> i1 >> j1 >> i2 >> j2;
i1--;
i2--;
j1--;
j2--;
vector<vector<int>> ban(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
ban[i][j] = (c == '@');
}
}
fill(d, d + 4 * n * m, INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
int ni = i + dx[k];
int nj = j + dy[k];
if (0 <= ni && ni < n && 0 <= nj && nj < m && !ban[i][j] &&
!ban[ni][nj]) {
g[i * m + j + k * n * m].push_back(ni * m + nj + k * n * m);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!ban[i][j]) {
for (int k = 0; k < 4; k++) {
g[i * m + j + k * n * m].push_back(i * m + j + ((k + 1) % 4) * n * m);
}
}
}
}
d[i1 * m + j1] = 0;
set<pair<int, int>> s;
s.insert({d[i1 * m + j1], i1 * m + j1});
while (!s.empty()) {
int v = s.begin()->second;
s.erase(s.begin());
// cerr << d[v] << endl;
for (auto t : g[v]) {
if (abs(t - v) % (n * m)) {
if (d[t] > d[v] + 1) {
s.erase({d[t], t});
d[t] = d[v] + 1;
s.insert({d[t], t});
}
} else {
if (d[t] > ((d[v] + K - 1) / K) * K) {
s.erase({d[t], t});
d[t] = ((d[v] + K - 1) / K) * K;
s.insert({d[t], t});
}
}
}
}
int ans = d[i2 * m + j2];
if (ans == INF) {
cout << -1 << endl;
} else {
cout << (ans + K - 1) / K << endl;
}
} | replace | 26 | 27 | 26 | 27 | 0 | |
p02644 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
vector<string> g(h);
rep(i, h) cin >> g[i];
vector<ll> dist(h * w * 4, INF);
ll start = ((x1 * w) + y1) * 4;
ll goal = ((x2 * w) + y2) * 4;
// cout<<start<<"s"<<goal<<"g"<<endl;
priority_queue<P, vector<P>, greater<P>> q;
rep(i, 4) {
ll id = start + i;
q.push(make_pair(0, id));
dist[id] = 0;
}
while (!q.empty()) {
auto Q = q.top();
q.pop();
ll d = Q.first;
ll id = Q.second;
// cout<<id<<"id"<<d<<"d"<<dist[id]<<"dist"<<endl;
if (dist[id] != d)
continue;
// dist[id]=d;
ll vnow = id % 4;
ll jnow = (id - vnow) / 4 % w;
ll inow = ((id - vnow) / 4 - jnow) / w;
ll D = ((d + k - 1) / k) * k;
// cout<<D<<"D"<<endl;
rep(i, 4) {
ll idnow = (inow * w + jnow) * 4 + i;
if (dist[idnow] > D) {
q.push(make_pair(D, idnow));
// cout<<D<<"D"<<idnow<<"ID"<<endl;
dist[idnow] = D;
}
ll ni = inow + dx[i];
ll nj = jnow + dy[i];
if (i != vnow)
continue;
if (ni < 0 or ni >= h or nj < 0 or nj > w)
continue;
if (g[ni][nj] == '@')
continue;
if (dist[(ni * w + nj) * 4 + i] <= (d + 1))
continue;
q.push(make_pair(d + 1, (ni * w + nj) * 4 + i));
// cout<<((ni*w+nj)*4+i)<<"nid"<<d+1<<"nd"<<endl;
dist[(ni * w + nj) * 4 + i] = d + 1;
}
}
ll ans = INF;
rep(i, 4) { ans = min(ans, dist[goal + i]); }
// cout<<ans;
if (ans == INF)
cout << -1;
else {
// cout<<ans<<endl;
cout << (ans + k - 1) / k;
}
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
x2--;
y1--;
y2--;
vector<string> g(h);
rep(i, h) cin >> g[i];
vector<ll> dist(h * w * 4, INF);
ll start = ((x1 * w) + y1) * 4;
ll goal = ((x2 * w) + y2) * 4;
// cout<<start<<"s"<<goal<<"g"<<endl;
priority_queue<P, vector<P>, greater<P>> q;
rep(i, 4) {
ll id = start + i;
q.push(make_pair(0, id));
dist[id] = 0;
}
while (!q.empty()) {
auto Q = q.top();
q.pop();
ll d = Q.first;
ll id = Q.second;
// cout<<id<<"id"<<d<<"d"<<dist[id]<<"dist"<<endl;
if (dist[id] != d)
continue;
// dist[id]=d;
ll vnow = id % 4;
ll jnow = (id - vnow) / 4 % w;
ll inow = ((id - vnow) / 4 - jnow) / w;
ll D = ((d + k - 1) / k) * k;
// cout<<D<<"D"<<endl;
rep(i, 4) {
ll idnow = (inow * w + jnow) * 4 + i;
if (dist[idnow] > D) {
q.push(make_pair(D, idnow));
// cout<<D<<"D"<<idnow<<"ID"<<endl;
dist[idnow] = D;
}
ll ni = inow + dx[i];
ll nj = jnow + dy[i];
if (i != vnow)
continue;
if (ni < 0 or ni >= h or nj < 0 or nj >= w)
continue;
if (g[ni][nj] == '@')
continue;
if (dist[(ni * w + nj) * 4 + i] <= (d + 1))
continue;
q.push(make_pair(d + 1, (ni * w + nj) * 4 + i));
// cout<<((ni*w+nj)*4+i)<<"nid"<<d+1<<"nd"<<endl;
dist[(ni * w + nj) * 4 + i] = d + 1;
}
}
ll ans = INF;
rep(i, 4) { ans = min(ans, dist[goal + i]); }
// cout<<ans;
if (ans == INF)
cout << -1;
else {
// cout<<ans<<endl;
cout << (ans + k - 1) / k;
}
}
| replace | 60 | 61 | 60 | 61 | 0 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K, x[2], y[2];
cin >> H >> W >> K;
REP(i, 2) {
cin >> x[i] >> y[i];
x[i]--;
y[i]--;
}
vector<string> c(H);
REP(i, H) { cin >> c[i]; }
vector<vector<int>> cost(H, vector<int>(W, INT_MAX));
cost[x[0]][y[0]] = 0;
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> pq;
pq.emplace(0, x[0], y[0]);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
while (!pq.empty()) {
int d, u, v;
tie(d, u, v) = pq.top();
pq.pop();
if (d > cost[u][v])
continue;
REP(i, 4) {
FOR(j, 1, K + 1) {
int s = u + j * dx[i];
int t = v + j * dy[i];
if (s >= 0 && s < H && t >= 0 && t < W) {
if (c[s][t] == '@')
break;
if (d + 1 < cost[s][t]) {
cost[s][t] = d + 1;
pq.emplace(cost[s][t], s, t);
}
}
}
}
}
if (cost[x[1]][y[1]] == INT_MAX) {
cout << -1 << endl;
} else {
cout << cost[x[1]][y[1]] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K, x[2], y[2];
cin >> H >> W >> K;
REP(i, 2) {
cin >> x[i] >> y[i];
x[i]--;
y[i]--;
}
vector<string> c(H);
REP(i, H) { cin >> c[i]; }
vector<vector<int>> cost(H, vector<int>(W, INT_MAX));
cost[x[0]][y[0]] = 0;
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> pq;
pq.emplace(0, x[0], y[0]);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
while (!pq.empty()) {
int d, u, v;
tie(d, u, v) = pq.top();
pq.pop();
if (d > cost[u][v])
continue;
REP(i, 4) {
FOR(j, 1, K + 1) {
int s = u + j * dx[i];
int t = v + j * dy[i];
if (s >= 0 && s < H && t >= 0 && t < W) {
if (c[s][t] == '@' || d + 1 > cost[s][t])
break;
if (d + 1 < cost[s][t]) {
cost[s][t] = d + 1;
pq.emplace(cost[s][t], s, t);
}
}
}
}
}
if (cost[x[1]][y[1]] == INT_MAX) {
cout << -1 << endl;
} else {
cout << cost[x[1]][y[1]] << endl;
}
return 0;
}
| replace | 47 | 48 | 47 | 48 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
// 四方向への移動ベクトル
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
int main() {
ll H, W, K;
cin >> H >> W >> K;
ll sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> field(H);
rep(i, H) cin >> field[i];
// 各セルの最短距離 (訪れていないところは -1 としておく)
vector<vector<int>> d(H, vector<int>(W, -1));
d[sx][sy] = 0; // スタートを 0 に
// そもそもいけない時はKでの探索しない
queue<P> que1;
que1.pu(mp(sx, sy));
while (!que1.empty()) {
pair<ll, ll> current_pos =
que1.front(); // キューの先頭を見る (C++ ではこれをしても pop しない)
ll x = current_pos.first;
ll y = current_pos.second;
que1.pop(); // キューから pop を忘れずに
// 隣接頂点を探索
for (ll direction = 0; direction < 4; ++direction) {
ll next_x = x + dx[direction];
ll next_y = y + dy[direction];
if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
continue; // 場外アウトならダメ
if (field[next_x][next_y] == '@')
continue; // 壁はダメ
// まだ見ていない頂点なら push
if (d[next_x][next_y] == -1) {
que1.push(make_pair(next_x, next_y));
d[next_x][next_y] = d[x][y] + 1; // (next_x, next_y) の距離も更新
}
}
}
if (d[gx][gy] == -1) {
cout << -1 << endl;
return 0;
}
// 各セルの最短距離 (訪れていないところは -1 としておく)
vector<vector<int>> dist(H, vector<int>(W, -1));
dist[sx][sy] = 0; // スタートを 0 に
queue<P> que;
que.pu(mp(sx, sy));
while (!que.empty()) {
pair<ll, ll> current_pos =
que.front(); // キューの先頭を見る (C++ ではこれをしても pop しない)
ll x = current_pos.first;
ll y = current_pos.second;
if (dist[x][y] > d[gx][gy])
break;
que.pop(); // キューから pop を忘れずに
// 隣接頂点を探索
for (ll direction = 0; direction < 4; ++direction) {
repn(j, K) {
ll next_x = x + j * dx[direction];
ll next_y = y + j * dy[direction];
if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
break; // 場外アウトならダメ
if (field[next_x][next_y] == '@')
break; // 壁はダメ
if (dist[next_x][next_y] > dist[x][y] + 1)
break;
if (dist[next_x][next_y] < dist[x][y]) {
if (dist[next_x][next_y] != -1) {
break;
}
}
// まだ見ていない頂点なら push
if (dist[next_x][next_y] == -1) {
que.push(make_pair(next_x, next_y));
dist[next_x][next_y] =
dist[x][y] + 1; // (next_x, next_y) の距離も更新
}
}
}
}
/*
rep(i,H){
rep(j,W){
cout << dist[i][j] << " ";
}
cout << endl;
}
*/
cout << dist[gx][gy] << endl;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
const int MAX = 510000;
const int MOD = 1000000007;
// 四方向への移動ベクトル
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};
int main() {
ll H, W, K;
cin >> H >> W >> K;
ll sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> field(H);
rep(i, H) cin >> field[i];
// 各セルの最短距離 (訪れていないところは -1 としておく)
vector<vector<int>> d(H, vector<int>(W, -1));
d[sx][sy] = 0; // スタートを 0 に
// そもそもいけない時はKでの探索しない
queue<P> que1;
que1.pu(mp(sx, sy));
while (!que1.empty()) {
pair<ll, ll> current_pos =
que1.front(); // キューの先頭を見る (C++ ではこれをしても pop しない)
ll x = current_pos.first;
ll y = current_pos.second;
que1.pop(); // キューから pop を忘れずに
// 隣接頂点を探索
for (ll direction = 0; direction < 4; ++direction) {
ll next_x = x + dx[direction];
ll next_y = y + dy[direction];
if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
continue; // 場外アウトならダメ
if (field[next_x][next_y] == '@')
continue; // 壁はダメ
// まだ見ていない頂点なら push
if (d[next_x][next_y] == -1) {
que1.push(make_pair(next_x, next_y));
d[next_x][next_y] = d[x][y] + 1; // (next_x, next_y) の距離も更新
}
}
}
if (d[gx][gy] == -1) {
cout << -1 << endl;
return 0;
}
// 各セルの最短距離 (訪れていないところは -1 としておく)
vector<vector<int>> dist(H, vector<int>(W, -1));
dist[sx][sy] = 0; // スタートを 0 に
queue<P> que;
que.pu(mp(sx, sy));
while (!que.empty()) {
pair<ll, ll> current_pos =
que.front(); // キューの先頭を見る (C++ ではこれをしても pop しない)
ll x = current_pos.first;
ll y = current_pos.second;
if (dist[x][y] > d[gx][gy])
break;
que.pop(); // キューから pop を忘れずに
// 隣接頂点を探索
for (ll direction = 0; direction < 4; ++direction) {
repn(j, K) {
ll next_x = x + j * dx[direction];
ll next_y = y + j * dy[direction];
if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
break; // 場外アウトならダメ
if (field[next_x][next_y] == '@')
break; // 壁はダメ
if (dist[next_x][next_y] > dist[x][y] + 1)
break;
if (dist[next_x][next_y] <= dist[x][y]) {
if (dist[next_x][next_y] != -1) {
break;
}
}
// まだ見ていない頂点なら push
if (dist[next_x][next_y] == -1) {
que.push(make_pair(next_x, next_y));
dist[next_x][next_y] =
dist[x][y] + 1; // (next_x, next_y) の距離も更新
}
}
}
}
/*
rep(i,H){
rep(j,W){
cout << dist[i][j] << " ";
}
cout << endl;
}
*/
cout << dist[gx][gy] << endl;
}
| replace | 111 | 112 | 111 | 112 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i-- > 0;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
#define popcount __builtin_popcountll
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> stack<T> &operator>>(stack<T> &s, T &t) {
t = s.top();
s.pop();
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T> queue<T> &operator>>(queue<T> &q, T &t) {
t = q.front();
q.pop();
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) {
t = q.top();
q.pop();
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
istream &operator>>(istream &s, _Bit_reference b) {
int a;
s >> a;
assert(a == 0 || a == 1);
b = a;
return s;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const deque<T> d) {
fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; }
return s;
}
template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) {
return b = b & t;
}
template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) {
return b = b ^ t;
}
template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) {
return b = b | t;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
template <typename T, typename U>
pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) {
return a = a + b;
}
template <typename T, typename U>
pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) {
return a = a - b;
}
void print(void) { cout << "\n"; }
void Print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << "\n"; }
template <typename T> void Print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
template <typename T, typename... U> void Print(T &&t, U &&...u) {
cout << t << " ";
Print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int e5 = 1e5;
const int e9 = 1e9;
const int MD = 1e9 + 7;
const int md = 998244353;
const ll e18 = 1e18;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T> T &chmax(T &a, T b) { return a = max(a, b); }
template <typename T> T &chmin(T &a, T b) { return a = min(a, b); }
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
ll maxflow(vector<mil> &E, int s, int t) {
ll z = 0;
vi b(E.size(), -1);
for (int i = 0;; ++i) {
static auto dfs = [&](int v, ll f, auto &dfs) -> ll {
if (v == t)
return f;
b[v] = i;
for (auto &p : E[v]) {
if (b[p.first] < i && p.second) {
if (ll r = dfs(p.first, min(f, p.second), dfs)) {
p.second -= r;
E[p.first][v] += r;
return r;
}
}
}
return 0;
};
ll x = dfs(s, ll(1e18), dfs);
z += x;
if (x == 0)
return z;
}
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
assert(a.size());
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
assert(a.size());
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LCS(vector<T> A, vector<T> B) {
int N = A.size(), M = B.size();
vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1));
fr(i, N) {
fr(j, M) {
if (A[i] == B[j]) {
d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}};
} else {
d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]);
}
}
}
vector<T> r;
for (pii p = {N, M}; d[p.first][p.second].first;
p = d[p.first][p.second].second) {
r << A[d[p.first][p.second].second.first];
}
Reverse(r);
return r;
}
str LCS(str S, str T) {
vector<char> s =
LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end()));
return str(s.begin(), s.end());
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, s;
void extend(int N) {
foor(i, p.size(), N) {
p << i;
s << 1;
}
}
public:
UnionFind(void) {}
UnionFind(int N) { extend(N - 1); }
int find(int i) {
extend(i);
return p[i] = p[i] == i ? i : find(p[i]);
}
void unite(int a, int b) {
extend(a);
extend(b);
if ((a = find(a)) != (b = find(b))) {
if (s[a] > s[b]) {
swap(a, b);
}
s[b] += s[a];
p[a] = b;
}
}
void unite(pii p) { return unite(p.first, p.second); }
bool same(int a, int b) {
extend(a);
extend(b);
return find(a) == find(b);
}
bool same(pii p) { return same(p.first, p.second); }
int size(int x) {
extend(x);
return s[find(x)];
}
};
ll MST(vector<pair<ll, pii>> &E) {
Sort(E);
UnionFind uf;
ll z = 0;
for (auto &e : E) {
if (!uf.same(e.second)) {
z += e.first;
uf.unite(e.second);
}
}
return z;
}
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) {
assert(x != 0);
return pow(x, p - 2, p);
}
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
assert(n < p);
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) {
return k < 0 || n < k ? 0ll : P(n, k) * f[k].second % M;
}
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
ll F(int n) { return n < 0 ? 0 : f[n].first; }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
T get(int i) { return S[i + B]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
class LCA {
vvi par;
vi dep;
public:
LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) {
function<void(int, int)> dfs = [&](int i, int p) {
for (int j : E[i])
if (j != p) {
par[0][j] = i;
dep[j] = dep[i] + 1;
dfs(j, i);
}
};
par[0][root] = root;
dfs(root, root);
fr(i, par.size() - 1) {
fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; }
}
}
int operator()(int a, int b) {
if (dep[a] > dep[b])
swap(a, b);
for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) {
if (t & 1) {
b = par[i][b];
}
}
if (a == b)
return a;
rf(i, par.size()) {
if (par[i][a] != par[i][b]) {
a = par[i][a];
b = par[i][b];
}
}
return par[0][a];
}
int dist(int a, int b) { return dep[a] + dep[b] - 2 * dep[operator()(a, b)]; }
};
vpli factor(ll N) {
vpli r;
for (ll i = 2; i * i <= N; ++i) {
if (N % i == 0) {
r << pli{i, 0};
while (N % i == 0) {
N /= i;
++r.back().second;
}
}
}
if (N > 1) {
r << pli{N, 1};
}
return r;
}
vl divisors(ll n) {
vl r;
ll m = sqrt(n);
foor(i, 1, m) if (n % i == 0) r << ll(i);
rf(i, r.size() - (m * m == n)) r << n / r[i];
return r;
}
vi SuffixArray(str S) {
int N = S.size();
vi rank(N + 1), tmp(N + 1), sa(N + 1);
fr(i, N) {
sa[i] = i;
rank[i] = S[i];
}
sa[N] = N;
rank[N] = -1;
int k;
auto cmp = [&](int &a, int &b) -> bool {
if (rank[a] != rank[b])
return rank[a] < rank[b];
return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1);
};
for (k = 1; k <= N; k *= 2) {
sort(all(sa), cmp);
tmp[sa[0]] = 0;
foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); }
rank = tmp;
}
return sa;
};
template <typename T, typename U> void sort2(vector<T> &a, vector<U> &b) {
int N = a.size();
assert(b.size() == N);
vector<pair<T, U>> v;
fr(i, N) { v << pair<T, U>{a[i], b[i]}; }
Sort(v);
fr(i, N) {
a[i] = v[i].first;
b[i] = v[i].second;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int y1, x1, y2, x2;
cin >> y1 >> x1 >> y2 >> x2;
--y1;
--x1;
--y2;
--x2;
vs C(H);
cin >> C;
vvi d(H, vi(W, e9));
d[y1][x1] = 0;
queue<int> q;
q << y1 << x1;
while (q.size()) {
int y, x;
q >> y >> x;
fr(k, 4) {
foor(l, 1, K) {
int Y = y + vi{-l, 0, 0, l}[k];
int X = x + vi{0, -l, l, 0}[k];
if (X < 0 || X >= W || Y < 0 || Y >= H || C[Y][X] == '@' ||
d[Y][X] <= d[y][x])
break;
d[Y][X] = d[y][x] + 1;
q << Y << X;
}
}
}
print(d[y2][x2] < e9 ? d[y2][x2] : -1);
return 0;
}
| #include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define foor(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, n) for (int i = (n); i-- > 0;)
#define roof(i, b, a) for (int i = (b); i >= (a); --i)
#define elsif else if
#define all(x) x.begin(), x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
#define popcount __builtin_popcountll
using namespace std;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef vector<vu> vvu;
typedef double dbl;
typedef vector<dbl> vd;
typedef vector<vd> vvd;
typedef string str;
typedef vector<str> vs;
typedef vector<vs> vvs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<int, int> mii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
typedef pair<dbl, dbl> pdd;
typedef vector<pdd> vpdd;
typedef map<dbl, dbl> mdd;
typedef pair<str, str> pss;
typedef vector<pss> vpss;
typedef map<str, str> mss;
typedef pair<int, ll> pil;
typedef vector<pil> vpil;
typedef map<int, ll> mil;
typedef pair<ll, int> pli;
typedef vector<pli> vpli;
typedef map<ll, int> mli;
typedef pair<dbl, int> pdi;
typedef vector<pdi> vpdi;
typedef map<dbl, int> mdi;
template <typename T> vector<T> &operator<<(vector<T> &v, const T t) {
v.push_back(t);
return v;
}
template <typename T> multiset<T> &operator<<(multiset<T> &m, const T t) {
m.insert(t);
return m;
}
template <typename T> set<T> &operator<<(set<T> &s, const T t) {
s.insert(t);
return s;
}
template <typename T> stack<T> &operator<<(stack<T> &s, const T t) {
s.push(t);
return s;
}
template <typename T> stack<T> &operator>>(stack<T> &s, T &t) {
t = s.top();
s.pop();
return s;
}
template <typename T> queue<T> &operator<<(queue<T> &q, const T t) {
q.push(t);
return q;
}
template <typename T> queue<T> &operator>>(queue<T> &q, T &t) {
t = q.front();
q.pop();
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator<<(PQ<T, vector<T>, U> &q, const T t) {
q.push(t);
return q;
}
template <typename T, typename U>
PQ<T, vector<T>, U> &operator>>(PQ<T, vector<T>, U> &q, T &t) {
t = q.top();
q.pop();
return q;
}
template <typename T, typename U>
istream &operator>>(istream &s, pair<T, U> &p) {
return s >> p.first >> p.second;
}
istream &operator>>(istream &s, _Bit_reference b) {
int a;
s >> a;
assert(a == 0 || a == 1);
b = a;
return s;
}
template <typename T> istream &operator>>(istream &s, vector<T> &v) {
fr(i, v.size()) { s >> v[i]; }
return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> p) {
return s << p.first << " " << p.second;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> v) {
fr(i, v.size()) { i ? s << " " << v[i] : s << v[i]; }
return s;
}
template <typename T> ostream &operator<<(ostream &s, const deque<T> d) {
fr(i, d.size()) { i ? s << " " << d[i] : s << d[i]; }
return s;
}
template <typename T> _Bit_reference operator&=(_Bit_reference b, T t) {
return b = b & t;
}
template <typename T> _Bit_reference operator^=(_Bit_reference b, T t) {
return b = b ^ t;
}
template <typename T> _Bit_reference operator|=(_Bit_reference b, T t) {
return b = b | t;
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return {a.first + b.first, a.second + b.second};
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return {a.first - b.first, a.second - b.second};
}
template <typename T, typename U>
pair<T, U> &operator+=(pair<T, U> &a, pair<T, U> b) {
return a = a + b;
}
template <typename T, typename U>
pair<T, U> &operator-=(pair<T, U> &a, pair<T, U> b) {
return a = a - b;
}
void print(void) { cout << "\n"; }
void Print(void) { cout << endl; }
template <typename T> void print(T t) { cout << t << "\n"; }
template <typename T> void Print(T t) { cout << t << endl; }
template <typename T, typename... U> void print(T &&t, U &&...u) {
cout << t << " ";
print(forward<U>(u)...);
}
template <typename T, typename... U> void Print(T &&t, U &&...u) {
cout << t << " ";
Print(forward<U>(u)...);
}
bool YN(bool b) {
print(b ? "YES" : "NO");
return b;
}
bool PI(bool b) {
print(b ? "POSSIBLE" : "IMPOSSIBLE");
return b;
}
bool Yn(bool b) {
print(b ? "Yes" : "No");
return b;
}
bool Pi(bool b) {
print(b ? "Possible" : "Impossible");
return b;
}
bool yn(bool b) {
print(b ? "yes" : "no");
return b;
}
bool pi(bool b) {
print(b ? "possible" : "impossible");
return b;
}
const int e5 = 1e5;
const int e9 = 1e9;
const int MD = 1e9 + 7;
const int md = 998244353;
const ll e18 = 1e18;
template <typename T> str to_string(const T &n) {
ostringstream s;
s << n;
return s.str();
}
template <typename T> T &chmax(T &a, T b) { return a = max(a, b); }
template <typename T> T &chmin(T &a, T b) { return a = min(a, b); }
template <typename T, typename U>
vector<pair<T, U>> dijkstra(const vector<vector<pair<T, U>>> &E, const U s,
const T inf) {
using P = pair<T, U>;
vector<P> d;
fr(i, E.size()) { d << P{inf, i}; }
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
template <typename T, typename U>
map<U, pair<T, U>> dijkstra(map<U, vector<pair<T, U>>> E, const U s,
const T inf) {
using P = pair<T, U>;
map<U, P> d;
for (pair<U, vector<P>> e : E) {
d[e.first] = P{inf, e.first};
}
PQ<P, vector<P>, greater<P>> pq;
pq << (d[s] = P{0, s});
while (pq.size()) {
P a = pq.top();
pq.pop();
U v = a.second;
if (d[v].first >= a.first) {
for (P e : E[v]) {
if (d[v].first + e.first < d[e.second].first) {
d[e.second] = P{d[v].first + e.first, v};
pq << P{d[v].first + e.first, e.second};
}
}
}
}
return d;
}
ll maxflow(vector<mil> &E, int s, int t) {
ll z = 0;
vi b(E.size(), -1);
for (int i = 0;; ++i) {
static auto dfs = [&](int v, ll f, auto &dfs) -> ll {
if (v == t)
return f;
b[v] = i;
for (auto &p : E[v]) {
if (b[p.first] < i && p.second) {
if (ll r = dfs(p.first, min(f, p.second), dfs)) {
p.second -= r;
E[p.first][v] += r;
return r;
}
}
}
return 0;
};
ll x = dfs(s, ll(1e18), dfs);
z += x;
if (x == 0)
return z;
}
}
template <typename T> T distsq(pair<T, T> a, pair<T, T> b) {
return (a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second);
}
template <typename T> T max(const vector<T> a) {
assert(a.size());
T m = a[0];
for (T e : a) {
m = max(m, e);
}
return m;
}
template <typename T> T min(const vector<T> a) {
assert(a.size());
T m = a[0];
for (T e : a) {
m = min(m, e);
}
return m;
}
template <typename T> T gcd(const T a, const T b) {
return a ? gcd(b % a, a) : b;
}
template <typename T> T gcd(const vector<T> a) {
T g = a[0];
for (T e : a) {
g = gcd(g, e);
}
return g;
}
template <typename T> vector<T> LCS(vector<T> A, vector<T> B) {
int N = A.size(), M = B.size();
vector<vector<pair<int, pii>>> d(N + 1, vector<pair<int, pii>>(M + 1));
fr(i, N) {
fr(j, M) {
if (A[i] == B[j]) {
d[i + 1][j + 1] = {d[i][j].first + 1, {i, j}};
} else {
d[i + 1][j + 1] = max(d[i][j + 1], d[i + 1][j]);
}
}
}
vector<T> r;
for (pii p = {N, M}; d[p.first][p.second].first;
p = d[p.first][p.second].second) {
r << A[d[p.first][p.second].second.first];
}
Reverse(r);
return r;
}
str LCS(str S, str T) {
vector<char> s =
LCS(vector<char>(S.begin(), S.end()), vector<char>(T.begin(), T.end()));
return str(s.begin(), s.end());
}
template <typename T> vector<pair<T, T>> ConvexHull(vector<pair<T, T>> V) {
if (V.size() <= 3) {
return V;
}
Sort(V);
rf(i, V.size() - 1) V << V[i];
vector<pair<T, T>> r;
for (pair<T, T> p : V) {
int s = r.size();
while (s >= 2 &&
(p.second - r[s - 1].second) * (p.first - r[s - 2].first) <
(p.second - r[s - 2].second) * (p.first - r[s - 1].first)) {
r.pop_back();
--s;
}
r << p;
}
r.pop_back();
return r;
}
class UnionFind {
vi p, s;
void extend(int N) {
foor(i, p.size(), N) {
p << i;
s << 1;
}
}
public:
UnionFind(void) {}
UnionFind(int N) { extend(N - 1); }
int find(int i) {
extend(i);
return p[i] = p[i] == i ? i : find(p[i]);
}
void unite(int a, int b) {
extend(a);
extend(b);
if ((a = find(a)) != (b = find(b))) {
if (s[a] > s[b]) {
swap(a, b);
}
s[b] += s[a];
p[a] = b;
}
}
void unite(pii p) { return unite(p.first, p.second); }
bool same(int a, int b) {
extend(a);
extend(b);
return find(a) == find(b);
}
bool same(pii p) { return same(p.first, p.second); }
int size(int x) {
extend(x);
return s[find(x)];
}
};
ll MST(vector<pair<ll, pii>> &E) {
Sort(E);
UnionFind uf;
ll z = 0;
for (auto &e : E) {
if (!uf.same(e.second)) {
z += e.first;
uf.unite(e.second);
}
}
return z;
}
ll strmod(const str &s, const int m) {
ll x = 0;
fr(i, s.size()) { x = (x * 10 + s[i] - 48) % m; }
return x;
}
vvl mul(const vvl &A, const vvl &B, const int m) {
vvl C;
fr(y, A.size()) { C << vl(B[y].size()); }
fr(y, C.size()) {
fr(x, C[y].size()) {
fr(i, A[0].size()) { (C[y][x] += A[y][i] * B[i][x]) %= m; }
}
}
return C;
}
vvl pow(const vvl &A, const ll n, const int m) {
vvl B;
fr(y, A.size()) { B << vl(A.size()); }
if (n == 0) {
fr(i, B.size()) { B[i][i] = 1; }
}
elsif(n % 2) { B = mul(A, pow(A, n - 1, m), m); }
else {
vvl C = pow(A, n / 2, m);
B = mul(C, C, m);
}
return B;
}
ll pow(const ll a, const ll n, const int m) {
ll t;
return n ? (n & 1 ? a >= 0 ? a % m : (m - (-a % m)) % m : 1) *
(t = pow(a, n >> 1, m), t * t % m) % m
: !!a;
}
ll inv(const ll x, const int p) {
assert(x != 0);
return pow(x, p - 2, p);
}
ll inv(const ll x) { return inv(x, MD); }
vpll fact(const int n, const int p) {
assert(n < p);
vpll v(n + 1);
v[0].first = 1;
foor(i, 1, n) { v[i].first = v[i - 1].first * i % p; }
v[n].second = inv(v[n].first, p);
roof(i, n, 1) { v[i - 1].second = v[i].second * i % p; }
return v;
}
class Combination {
const vpll f;
const int M;
public:
Combination(int n, int m) : f(fact(n, m)), M(m) {}
ll P(int n, int k) {
return n < 0 || k < 0 || n < k ? 0ll : f[n].first * f[n - k].second % M;
}
ll C(int n, int k) {
return k < 0 || n < k ? 0ll : P(n, k) * f[k].second % M;
}
ll H(int n, int k) { return n == 0 && k == 0 ? 1ll : C(n + k - 1, k); }
ll F(int n) { return n < 0 ? 0 : f[n].first; }
};
ll C2(const int n) { return (ll)n * ~-n / 2; }
ll sum(const vi a) {
ll s = 0;
for (int e : a) {
s += e;
}
return s;
}
ll sum(const vl a) {
ll s = 0;
for (ll e : a) {
s += e;
}
return s;
}
template <typename T> int MSB(T N) {
int r = -1;
for (; N > 0; N /= 2) {
++r;
}
return r;
}
template <typename T> class SegmentTree {
vector<T> S;
T (*const op)(T a, T b);
const T zero;
const int B;
public:
SegmentTree(int N, T (*f)(T a, T b), const T zero)
: S(1 << MSB(N - 1) + 2, zero), op(f), zero(zero),
B(1 << MSB(N - 1) + 1) {}
SegmentTree(vector<T> v, T (*f)(T a, T b), const T zero)
: SegmentTree(v.size(), f, zero) {
fr(i, v.size()) { S[S.size() / 2 + i] = v[i]; }
roof(i, S.size() / 2 - 1, 1) { S[i] = op(S[i * 2], S[i * 2 + 1]); }
}
T calc(int l, int r) {
l += B;
r += B;
if (l > r) {
return zero;
}
if (l == r) {
return S[l];
}
T L = S[l], R = S[r];
for (; l / 2 < r / 2; l /= 2, r /= 2) {
if (l % 2 == 0) {
L = op(L, S[l + 1]);
}
if (r % 2 == 1) {
R = op(S[r - 1], R);
}
}
return op(L, R);
}
void replace(int i, T x) {
for (S[i += B] = x; i != 1; i /= 2) {
if (i % 2) {
S[i / 2] = op(S[i - 1], S[i]);
} else {
S[i / 2] = op(S[i], S[i + 1]);
}
}
}
void add(int i, T x) { replace(i, op(S[B + i], x)); }
T top() { return S[1]; }
T get(int i) { return S[i + B]; }
};
ll BITsum(vl &B, int i) {
ll z = 0;
while (i > 0) {
z += B[i];
i -= i & -i;
}
return z;
}
void BITadd(vl &B, int i, ll x) {
while (i < B.size()) {
B[i] += x;
i += i & -i;
}
}
ll fib(const ll n, const int m) {
ll a, b, c, d, A, B, C, D;
a = 1;
b = 0;
c = 0;
d = 1;
rf(i, 63) {
A = a * a + b * c;
B = a * b + b * d;
C = c * a + d * c;
D = c * b + d * d;
if (n >> i & 1) {
a = A;
b = B;
c = C;
d = D;
A = a + b;
B = a;
C = c + d;
D = c;
}
a = A % m;
b = B % m;
c = C % m;
d = D % m;
}
return b;
}
vi primes(int n) {
vb b(n + 1);
vi p;
foor(i, 2, n) {
if (!b[i]) {
p << i;
for (int j = 2 * i; j <= n; j += i) {
b[j] = true;
}
}
}
return p;
}
vb isprime(const int n) {
vb v(n + 1, true);
v[0] = v[1] = false;
foor(i, 2, n) {
if (v[i]) {
for (int j = 2 * i; j <= n; j += i) {
v[j] = false;
}
}
}
return v;
}
class LCA {
vvi par;
vi dep;
public:
LCA(vvi &E, int root) : par(MSB(E.size()) + 1, vi(E.size())), dep(E.size()) {
function<void(int, int)> dfs = [&](int i, int p) {
for (int j : E[i])
if (j != p) {
par[0][j] = i;
dep[j] = dep[i] + 1;
dfs(j, i);
}
};
par[0][root] = root;
dfs(root, root);
fr(i, par.size() - 1) {
fr(j, par[0].size()) { par[i + 1][j] = par[i][par[i][j]]; }
}
}
int operator()(int a, int b) {
if (dep[a] > dep[b])
swap(a, b);
for (int t = dep[b] - dep[a], i = 0; t; t >>= 1, ++i) {
if (t & 1) {
b = par[i][b];
}
}
if (a == b)
return a;
rf(i, par.size()) {
if (par[i][a] != par[i][b]) {
a = par[i][a];
b = par[i][b];
}
}
return par[0][a];
}
int dist(int a, int b) { return dep[a] + dep[b] - 2 * dep[operator()(a, b)]; }
};
vpli factor(ll N) {
vpli r;
for (ll i = 2; i * i <= N; ++i) {
if (N % i == 0) {
r << pli{i, 0};
while (N % i == 0) {
N /= i;
++r.back().second;
}
}
}
if (N > 1) {
r << pli{N, 1};
}
return r;
}
vl divisors(ll n) {
vl r;
ll m = sqrt(n);
foor(i, 1, m) if (n % i == 0) r << ll(i);
rf(i, r.size() - (m * m == n)) r << n / r[i];
return r;
}
vi SuffixArray(str S) {
int N = S.size();
vi rank(N + 1), tmp(N + 1), sa(N + 1);
fr(i, N) {
sa[i] = i;
rank[i] = S[i];
}
sa[N] = N;
rank[N] = -1;
int k;
auto cmp = [&](int &a, int &b) -> bool {
if (rank[a] != rank[b])
return rank[a] < rank[b];
return (a + k <= N ? rank[a + k] : -1) < (b + k <= N ? rank[b + k] : -1);
};
for (k = 1; k <= N; k *= 2) {
sort(all(sa), cmp);
tmp[sa[0]] = 0;
foor(i, 1, N) { tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]); }
rank = tmp;
}
return sa;
};
template <typename T, typename U> void sort2(vector<T> &a, vector<U> &b) {
int N = a.size();
assert(b.size() == N);
vector<pair<T, U>> v;
fr(i, N) { v << pair<T, U>{a[i], b[i]}; }
Sort(v);
fr(i, N) {
a[i] = v[i].first;
b[i] = v[i].second;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
int y1, x1, y2, x2;
cin >> y1 >> x1 >> y2 >> x2;
--y1;
--x1;
--y2;
--x2;
vs C(H);
cin >> C;
vvi d(H, vi(W, e9));
d[y1][x1] = 0;
queue<int> q;
q << y1 << x1;
while (q.size()) {
int y, x;
q >> y >> x;
fr(k, 4) {
foor(l, 1, K) {
int Y = y + vi{-l, 0, 0, l}[k];
int X = x + vi{0, -l, l, 0}[k];
if (X < 0 || X >= W || Y < 0 || Y >= H || C[Y][X] == '@' ||
d[Y][X] <= d[y][x])
break;
if (d[Y][X] > d[y][x] + 1) {
d[Y][X] = d[y][x] + 1;
q << Y << X;
}
}
}
}
print(d[y2][x2] < e9 ? d[y2][x2] : -1);
return 0;
}
| replace | 716 | 718 | 716 | 720 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &p : a) {
out << (first ? "" : ", ");
out << p.first << ":" << p.second;
first = 0;
}
out << "}";
return out;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
while (b) {
T tmp = b;
b = a % b;
a = tmp;
}
return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
long long qpow(long long a, long long b, int MOD) {
if (b == 0)
return 1;
long long res = 1;
while (b) {
if (b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
const int MOD = 1e9 + 7;
const int maxn = 2e6;
const int INF = 1 << 30;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
fac[0] = 1;
for (int i = 1; i <= maxn; i++)
fac[i] = 1LL * fac[i - 1] * i % MOD;
invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
for (int i = maxn - 1; i >= 0; i--)
invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
assert(0 <= k && n >= k);
int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
return (int)(1LL * fac[n] * tmp % MOD);
}
// struct Node {
// int x, y, pdi, step, pd;
// };
// bool operator>(Node& a, Node& b) {
// return a.pd < b.pd;
// }
struct Node {
int x, y, pdi, step, pd;
Node(int xx, int yy, int pdii, int stepp, int pdd) {
x = xx;
y = yy;
pdi = pdii;
step = stepp;
pd = pdd;
}
bool operator<(const Node &other) const { return pd > other.pd; }
};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> mat(n);
for (int i = 0; i < n; i++) {
cin >> mat[i];
}
vector<vector<int>> d(n, vector<int>(m, INF));
d[x1][y1] = 0;
queue<PII> q;
q.emplace(x1, y1);
while (!q.empty()) {
PII p = q.front();
q.pop();
int x = p.first, y = p.second;
for (int di = 0; di < 4; di++) {
int ncost = d[x][y] + 1;
for (int i = 1; i <= k; i++) {
int nx = x + dx[di] * i, ny = y + dy[di] * i;
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
break;
if (mat[nx][ny] == '@')
break;
if (d[nx][ny] > ncost) {
d[nx][ny] = ncost;
q.emplace(nx, ny);
}
}
}
}
// trace(d);
cout << (d[x2][y2] == INF ? -1 : d[x2][y2]) << '\n';
// Dijkstra
// vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
// INF))); vector<vector<vector<int>>> vis(n, vector<vector<int>>(m,
// vector<int>(4, 0))); priority_queue<Node> q; for (int di = 0; di < 4; di++)
// {
// vis[x1][y1][di] = 1;
// int nx = x1 + dx[di], ny = y1 + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// Node tmp(nx, ny, di, 1, 1);
// q.push(tmp);
// vis[nx][ny][di] = 1;
// d[nx][ny][di] = 1;
// }
// bool ok = false;
// int ans = INF;
// while (!q.empty()) {
// Node tmp = q.top(); q.pop();
// int x = tmp.x, y = tmp.y, pdi = tmp.pdi, step = tmp.step, pd = tmp.pd;
// // trace(x, y, pdi, step, pd);
// if (pd >= ans) break;
// for (int di = 0; di < 4; di++) {
// int nx = x + dx[di], ny = y + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// if (vis[nx][ny][di]) continue;
// vis[nx][ny][di] = 1;
// int add = (step == k ? 1 : (pdi == di ? 0 : 1));
// int nd = pd + add;
// if (nx == x2 && ny == y2 && ans > nd) {
// ok = true;
// ans = nd;
// continue;
// }
// Node tmp2(nx, ny, di, (step == k ? 1 : (pdi == di ? step + 1 : 1)),
// nd); q.push(tmp2);
// }
// }
// if (!ok) {cout << -1 << '\n';}
// else cout << ans << '\n';
// deque<pair<PII, PII>> q;
// vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
// INF))); for (int di = 0; di < 4; di++) d[x1][y1][di] = 0; for (int di = 0;
// di < 4; di++) {
// int nx1 = x1 + dx[di], ny1 = y1 + dy[di];
// if (nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= m) continue;
// if (mat[nx1][ny1] == '@') continue;
// q.emplace_back(make_pair(nx1, ny1), make_pair(di, 1));
// d[nx1][ny1][di] = 1;
// }
// while (!q.empty()) {
// auto p = q.front(); q.pop_front();
// PII xy = p.first, did = p.second;
// int x = xy.first, y = xy.second, pdi = did.first, pd = did.second;
// for (int di = 0; di < 4; di++) {
// int nx = x + dx[di], ny = y + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// int pred = d[x][y][pdi];
// int add = (pd == k) ? 1 : ((pdi == di) ? 0 : 1);
// if (d[nx][ny][di] > pred + add) {
// d[nx][ny][di] = pred + add;
// q.emplace_back(make_pair(nx, ny), make_pair(di, (pd == k) ? 1 :
// (pdi == di ? pd + 1 : 1)));
// }
// }
// }
// // trace(d);
// int ans = INF;
// for (int di = 0; di < 4; di++) ans = min(ans, d[x2][y2][di]);
// cout << (ans == INF ? -1 : ans) << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
out << "[";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
out << "{";
bool first = true;
for (auto &p : a) {
out << (first ? "" : ", ");
out << p.first << ":" << p.second;
first = 0;
}
out << "}";
return out;
}
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T> T gcd(T a, T b) {
while (b) {
T tmp = b;
b = a % b;
a = tmp;
}
return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
long long qpow(long long a, long long b, int MOD) {
if (b == 0)
return 1;
long long res = 1;
while (b) {
if (b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
const int MOD = 1e9 + 7;
const int maxn = 2e6;
const int INF = 1 << 30;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
fac[0] = 1;
for (int i = 1; i <= maxn; i++)
fac[i] = 1LL * fac[i - 1] * i % MOD;
invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
for (int i = maxn - 1; i >= 0; i--)
invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
assert(0 <= k && n >= k);
int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
return (int)(1LL * fac[n] * tmp % MOD);
}
// struct Node {
// int x, y, pdi, step, pd;
// };
// bool operator>(Node& a, Node& b) {
// return a.pd < b.pd;
// }
struct Node {
int x, y, pdi, step, pd;
Node(int xx, int yy, int pdii, int stepp, int pdd) {
x = xx;
y = yy;
pdi = pdii;
step = stepp;
pd = pdd;
}
bool operator<(const Node &other) const { return pd > other.pd; }
};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m, k;
cin >> n >> m >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> mat(n);
for (int i = 0; i < n; i++) {
cin >> mat[i];
}
vector<vector<int>> d(n, vector<int>(m, INF));
d[x1][y1] = 0;
queue<PII> q;
q.emplace(x1, y1);
while (!q.empty()) {
PII p = q.front();
q.pop();
int x = p.first, y = p.second;
for (int di = 0; di < 4; di++) {
int ncost = d[x][y] + 1;
for (int i = 1; i <= k; i++) {
int nx = x + dx[di] * i, ny = y + dy[di] * i;
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
break;
if (mat[nx][ny] == '@')
break;
if (d[nx][ny] > ncost) {
d[nx][ny] = ncost;
q.emplace(nx, ny);
} else if (d[nx][ny] == ncost)
continue;
else
break;
}
}
}
// trace(d);
cout << (d[x2][y2] == INF ? -1 : d[x2][y2]) << '\n';
// Dijkstra
// vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
// INF))); vector<vector<vector<int>>> vis(n, vector<vector<int>>(m,
// vector<int>(4, 0))); priority_queue<Node> q; for (int di = 0; di < 4; di++)
// {
// vis[x1][y1][di] = 1;
// int nx = x1 + dx[di], ny = y1 + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// Node tmp(nx, ny, di, 1, 1);
// q.push(tmp);
// vis[nx][ny][di] = 1;
// d[nx][ny][di] = 1;
// }
// bool ok = false;
// int ans = INF;
// while (!q.empty()) {
// Node tmp = q.top(); q.pop();
// int x = tmp.x, y = tmp.y, pdi = tmp.pdi, step = tmp.step, pd = tmp.pd;
// // trace(x, y, pdi, step, pd);
// if (pd >= ans) break;
// for (int di = 0; di < 4; di++) {
// int nx = x + dx[di], ny = y + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// if (vis[nx][ny][di]) continue;
// vis[nx][ny][di] = 1;
// int add = (step == k ? 1 : (pdi == di ? 0 : 1));
// int nd = pd + add;
// if (nx == x2 && ny == y2 && ans > nd) {
// ok = true;
// ans = nd;
// continue;
// }
// Node tmp2(nx, ny, di, (step == k ? 1 : (pdi == di ? step + 1 : 1)),
// nd); q.push(tmp2);
// }
// }
// if (!ok) {cout << -1 << '\n';}
// else cout << ans << '\n';
// deque<pair<PII, PII>> q;
// vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(4,
// INF))); for (int di = 0; di < 4; di++) d[x1][y1][di] = 0; for (int di = 0;
// di < 4; di++) {
// int nx1 = x1 + dx[di], ny1 = y1 + dy[di];
// if (nx1 < 0 || nx1 >= n || ny1 < 0 || ny1 >= m) continue;
// if (mat[nx1][ny1] == '@') continue;
// q.emplace_back(make_pair(nx1, ny1), make_pair(di, 1));
// d[nx1][ny1][di] = 1;
// }
// while (!q.empty()) {
// auto p = q.front(); q.pop_front();
// PII xy = p.first, did = p.second;
// int x = xy.first, y = xy.second, pdi = did.first, pd = did.second;
// for (int di = 0; di < 4; di++) {
// int nx = x + dx[di], ny = y + dy[di];
// if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
// if (mat[nx][ny] == '@') continue;
// int pred = d[x][y][pdi];
// int add = (pd == k) ? 1 : ((pdi == di) ? 0 : 1);
// if (d[nx][ny][di] > pred + add) {
// d[nx][ny][di] = pred + add;
// q.emplace_back(make_pair(nx, ny), make_pair(di, (pd == k) ? 1 :
// (pdi == di ? pd + 1 : 1)));
// }
// }
// }
// // trace(d);
// int ans = INF;
// for (int di = 0; di < 4; di++) ans = min(ans, d[x2][y2][di]);
// cout << (ans == INF ? -1 : ans) << '\n';
return 0;
}
| replace | 195 | 196 | 195 | 199 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 5000005;
int n, m, k, a[N], dis[N], xs, ys, xt, yt;
int xy[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char str[N];
void bfs() {
memset(dis, -1, sizeof(dis));
queue<pair<int, int>> q;
q.push(make_pair(xs, ys));
dis[xs * m - m + ys] = 0;
while (!q.empty()) {
int xu = q.front().first;
int yu = q.front().second;
int u = xu * m - m + yu;
q.pop();
// cout<<xu<<' '<<yu<<' '<<dis[u]<<endl;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int xv = xu + xy[i][0] * j;
int yv = yu + xy[i][1] * j;
int v = xv * m - m + yv;
// cout<<xv<<' '<<yv<<' '<<v<<endl;
if (xv < 1 || xv > n || yv < 1 || yv > m)
break;
if (!a[v] || dis[v] <= dis[u] && dis[v] > -1)
break;
dis[v] = dis[u] + 1, q.push(make_pair(xv, yv));
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
scanf("%d%d%d%d", &xs, &ys, &xt, &yt);
for (int i = 1; i <= n; i++) {
scanf("%s", str + 1);
for (int j = 1; j <= m; j++) {
a[i * m - m + j] = (str[j] == '.');
}
}
bfs();
printf("%d\n", dis[xt * m - m + yt]);
} | #include <bits/stdc++.h>
using namespace std;
const int N = 5000005;
int n, m, k, a[N], dis[N], xs, ys, xt, yt;
int xy[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char str[N];
void bfs() {
memset(dis, -1, sizeof(dis));
queue<pair<int, int>> q;
q.push(make_pair(xs, ys));
dis[xs * m - m + ys] = 0;
while (!q.empty()) {
int xu = q.front().first;
int yu = q.front().second;
int u = xu * m - m + yu;
q.pop();
// cout<<xu<<' '<<yu<<' '<<dis[u]<<endl;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int xv = xu + xy[i][0] * j;
int yv = yu + xy[i][1] * j;
int v = xv * m - m + yv;
// cout<<xv<<' '<<yv<<' '<<v<<endl;
if (xv < 1 || xv > n || yv < 1 || yv > m)
break;
if (!a[v] || dis[v] <= dis[u] && dis[v] > -1)
break;
if (dis[v] > -1)
continue;
dis[v] = dis[u] + 1, q.push(make_pair(xv, yv));
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
scanf("%d%d%d%d", &xs, &ys, &xt, &yt);
for (int i = 1; i <= n; i++) {
scanf("%s", str + 1);
for (int j = 1; j <= m; j++) {
a[i * m - m + j] = (str[j] == '.');
}
}
bfs();
printf("%d\n", dis[xt * m - m + yt]);
} | insert | 30 | 30 | 30 | 32 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define pf printf
#define sc scanf
#define sf(n) scanf("%d", &n)
#define sff(n1, n2) scanf("%d %d", &n1, &n2)
#define sfff(n1, n2, n3) scanf("%d %d %d", &n1, &n2, &n3)
#define sl(n) scanf("%lld", &n)
#define sll(n1, n2) scanf("%lld %lld", &n1, &n2)
#define slll(n1, n2, n3) scanf("%lld %lld %lld", &n1, &n2, &n3)
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(i, n) for (i = 1; i <= n; i++)
#define reps(i, a, n) for (i = a; i <= n; i++)
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define sz(x) x.size()
#define mem(ara, n) memset(ara, n, sizeof(ara))
#define memb(ara) memset(ara, false, sizeof(ara))
#define pi pair<int, int>
#define pii pair<pair<int, int>, pair<int, int>>
#define endl "\n";
#define line puts("-------");
#define dbb(x) cout << #x << " : " << x << "\n";
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const queue<T> &x) {
queue<T> temp = x;
cerr << "\n-----\n";
while (!temp.empty()) {
cerr << temp.front() << endl;
temp.pop();
}
cerr << "-----\n";
}
template <typename T> void __print(const stack<T> &x) {
stack<T> temp = x;
cerr << "\n-----\n";
while (!temp.empty()) {
cerr << temp.top() << endl;
temp.pop();
}
cerr << "-----\n";
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define db(x...) cerr << "[" << #x << "] = [", _print(x)
#endif
using ll = long long;
const int N = 100005;
const int MOD = 1000000007;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int p, q, pp, qq, n, m, k;
string a[N];
bool valid(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '.')
return true;
return false;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif
FAST;
cin >> n >> m >> k >> p >> q >> pp >> qq;
cin.ignore();
int i, j;
rep0(i, n) cin >> a[i];
if (p == pp && q == qq) {
cout << 0;
return 0;
}
bool vis[n + 1][m + 1];
int cost[n + 1][m + 1];
rep0(i, n + 1) {
rep0(j, m + 1) {
vis[i][j] = false;
cost[i][j] = 0;
}
}
p--;
q--;
pp--;
qq--;
vis[p][q] = true;
cost[p][q] = 0;
queue<pi> qqq;
qqq.push({p, q});
while (!qqq.empty()) {
pi pr = qqq.front();
int x = pr.fi, y = pr.se;
if (x == pp && y == qq) {
cout << cost[pp][qq];
return 0;
}
qqq.pop();
rep0(i, 4) {
rep(j, k) {
int xx = x + dx[i] * j;
int yy = y + dy[i] * j;
if (!valid(xx, yy))
break;
if (!vis[xx][yy]) {
vis[xx][yy] = true;
cost[xx][yy] = cost[x][y] + 1;
if (xx == pp && yy == qq) {
cout << cost[pp][qq];
return 0;
}
qqq.push({xx, yy});
}
}
}
}
cout << -1;
}
| #include <bits/stdc++.h>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define pf printf
#define sc scanf
#define sf(n) scanf("%d", &n)
#define sff(n1, n2) scanf("%d %d", &n1, &n2)
#define sfff(n1, n2, n3) scanf("%d %d %d", &n1, &n2, &n3)
#define sl(n) scanf("%lld", &n)
#define sll(n1, n2) scanf("%lld %lld", &n1, &n2)
#define slll(n1, n2, n3) scanf("%lld %lld %lld", &n1, &n2, &n3)
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(i, n) for (i = 1; i <= n; i++)
#define reps(i, a, n) for (i = a; i <= n; i++)
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define sz(x) x.size()
#define mem(ara, n) memset(ara, n, sizeof(ara))
#define memb(ara) memset(ara, false, sizeof(ara))
#define pi pair<int, int>
#define pii pair<pair<int, int>, pair<int, int>>
#define endl "\n";
#define line puts("-------");
#define dbb(x) cout << #x << " : " << x << "\n";
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const queue<T> &x) {
queue<T> temp = x;
cerr << "\n-----\n";
while (!temp.empty()) {
cerr << temp.front() << endl;
temp.pop();
}
cerr << "-----\n";
}
template <typename T> void __print(const stack<T> &x) {
stack<T> temp = x;
cerr << "\n-----\n";
while (!temp.empty()) {
cerr << temp.top() << endl;
temp.pop();
}
cerr << "-----\n";
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define db(x...) cerr << "[" << #x << "] = [", _print(x)
#endif
using ll = long long;
const int N = 100005;
const int MOD = 1000000007;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int p, q, pp, qq, n, m, k;
string a[N];
bool valid(int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '.')
return true;
return false;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif
FAST;
cin >> n >> m >> k >> p >> q >> pp >> qq;
cin.ignore();
int i, j;
rep0(i, n) cin >> a[i];
if (p == pp && q == qq) {
cout << 0;
return 0;
}
bool vis[n + 1][m + 1];
int cost[n + 1][m + 1];
rep0(i, n + 1) {
rep0(j, m + 1) {
vis[i][j] = false;
cost[i][j] = 0;
}
}
p--;
q--;
pp--;
qq--;
vis[p][q] = true;
cost[p][q] = 0;
queue<pi> qqq;
qqq.push({p, q});
while (!qqq.empty()) {
pi pr = qqq.front();
int x = pr.fi, y = pr.se;
if (x == pp && y == qq) {
cout << cost[pp][qq];
return 0;
}
qqq.pop();
rep0(i, 4) {
rep(j, k) {
int xx = x + dx[i] * j;
int yy = y + dy[i] * j;
if (!valid(xx, yy))
break;
if (!vis[xx][yy]) {
vis[xx][yy] = true;
cost[xx][yy] = cost[x][y] + 1;
if (xx == pp && yy == qq) {
cout << cost[pp][qq];
return 0;
}
qqq.push({xx, yy});
} else {
if (cost[xx][yy] >= cost[x][y] + 1)
cost[xx][yy] = cost[x][y] + 1;
else
break;
}
}
}
}
cout << -1;
}
| insert | 159 | 159 | 159 | 164 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// conversion
//------------------------------------------
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();
}
// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }
// typedef
//------------------------------------------
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQGI; // 大きい順
typedef priority_queue<int, VI, greater<int>> PQLI;
typedef priority_queue<PII> PQGP;
typedef priority_queue<PII, VPII, greater<int>> PQLP;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EB emplace_back
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define LB lower_bound
#define UB upper_bound
#define NEXP next_permutation
#define FI first
#define SE second
#define Vmin(a) *min_element((a).begin(), (a).end())
#define Vmax(a) *max_element((a).begin(), (a).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REPR(i, n) FORR(i, 0, n)
#define CFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define CREP(i, n) CFOR(i, 0, n)
#define CFORR(i, a, b) for (int i = (b); i >= (a); i--)
#define CREPR(i, n) CFORR(i, 0, n)
#define BFOR(bit, a, b) for (int bit = (a); bit < (1 << (b)); ++bit)
#define BREP(bit, n) BFOR(bit, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const LL LINF = LLONG_MAX / 3;
const int RINF = INT_MIN / 2;
const LL RLINF = LLONG_MIN / 3;
const LL MOD = 1e9 + 7;
const LL MODD = 998244353;
const int MAX = 510000;
inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }
// other
//-------------------------------------------
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define COUT(x) cout << (x) << endl
#define COUT2(x, y) cout << (x) << " " << (y) << endl
#define COUT3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define PR(x) cout << (x)
#define ENDL cout << endl
#define SPACE cout << " "
#define BC(x) __builtin_popcountll(x)
VI dx = {1, 0, -1, 0, 1, 1, -1, -1};
VI dy = {0, 1, 0, -1, 1, -1, 1, -1};
LL Gcd(LL a, LL b) { return __gcd(a, b); }
LL Lcm(LL a, LL b) { return a / Gcd(a, b) * b; }
LL ModPow(LL A, LL N, LL M) {
LL res = 1;
while (N > 0) {
if (N & 1)
res = res * A % M;
A = A * A % M;
N >>= 1;
}
return res;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
struct node {
int x, y, l;
node(int x, int y, int l) : x(x), y(y), l(l){};
};
int main() {
// cin.tie(0);
// ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int H, W, K;
cin >> H >> W >> K;
PII s, g;
cin >> s >> g;
s.FI--;
s.SE--;
g.FI--;
g.SE--;
VS C(H);
cin >> C;
VVI len(H, VI(W, INF));
REP(i, H) REP(j, W) {
if (C.at(i).at(j) == '@')
len.at(i).at(j) = -1;
}
len.at(s.FI).at(s.SE) = 0;
queue<node> qu;
qu.emplace(s.FI, s.SE, 0);
while (!qu.empty()) {
node now = qu.front();
qu.pop();
REP(i, 4) {
for (int t = 1; t <= K; t++) {
int x = now.x + dx.at(i) * t;
int y = now.y + dy.at(i) * t;
if (x < 0 || H <= x || y < 0 || W <= y)
break;
if (len.at(x).at(y) >= now.l) {
chmin(len.at(x).at(y), now.l + 1);
qu.emplace(x, y, now.l + 1);
} else {
break;
}
}
}
}
int ans = len.at(g.FI).at(g.SE);
if (ans == INF)
ans = -1;
COUT(ans);
// REP(i,H)COUT(len.at(i));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// conversion
//------------------------------------------
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();
}
// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }
// typedef
//------------------------------------------
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQGI; // 大きい順
typedef priority_queue<int, VI, greater<int>> PQLI;
typedef priority_queue<PII> PQGP;
typedef priority_queue<PII, VPII, greater<int>> PQLP;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EB emplace_back
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) \
for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define LB lower_bound
#define UB upper_bound
#define NEXP next_permutation
#define FI first
#define SE second
#define Vmin(a) *min_element((a).begin(), (a).end())
#define Vmax(a) *max_element((a).begin(), (a).end())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REPR(i, n) FORR(i, 0, n)
#define CFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define CREP(i, n) CFOR(i, 0, n)
#define CFORR(i, a, b) for (int i = (b); i >= (a); i--)
#define CREPR(i, n) CFORR(i, 0, n)
#define BFOR(bit, a, b) for (int bit = (a); bit < (1 << (b)); ++bit)
#define BREP(bit, n) BFOR(bit, 0, n)
// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const LL LINF = LLONG_MAX / 3;
const int RINF = INT_MIN / 2;
const LL RLINF = LLONG_MIN / 3;
const LL MOD = 1e9 + 7;
const LL MODD = 998244353;
const int MAX = 510000;
inline bool Eq(double a, double b) { return fabs(b - a) < EPS; }
// other
//-------------------------------------------
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#define COUT(x) cout << (x) << endl
#define COUT2(x, y) cout << (x) << " " << (y) << endl
#define COUT3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define PR(x) cout << (x)
#define ENDL cout << endl
#define SPACE cout << " "
#define BC(x) __builtin_popcountll(x)
VI dx = {1, 0, -1, 0, 1, 1, -1, -1};
VI dy = {0, 1, 0, -1, 1, -1, 1, -1};
LL Gcd(LL a, LL b) { return __gcd(a, b); }
LL Lcm(LL a, LL b) { return a / Gcd(a, b) * b; }
LL ModPow(LL A, LL N, LL M) {
LL res = 1;
while (N > 0) {
if (N & 1)
res = res * A % M;
A = A * A % M;
N >>= 1;
}
return res;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v)
is >> in;
return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
struct node {
int x, y, l;
node(int x, int y, int l) : x(x), y(y), l(l){};
};
int main() {
// cin.tie(0);
// ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
int H, W, K;
cin >> H >> W >> K;
PII s, g;
cin >> s >> g;
s.FI--;
s.SE--;
g.FI--;
g.SE--;
VS C(H);
cin >> C;
VVI len(H, VI(W, INF));
REP(i, H) REP(j, W) {
if (C.at(i).at(j) == '@')
len.at(i).at(j) = -1;
}
len.at(s.FI).at(s.SE) = 0;
queue<node> qu;
qu.emplace(s.FI, s.SE, 0);
while (!qu.empty()) {
node now = qu.front();
qu.pop();
REP(i, 4) {
for (int t = 1; t <= K; t++) {
int x = now.x + dx.at(i) * t;
int y = now.y + dy.at(i) * t;
if (x < 0 || H <= x || y < 0 || W <= y)
break;
if (len.at(x).at(y) > now.l) {
if (chmin(len.at(x).at(y), now.l + 1)) {
qu.emplace(x, y, now.l + 1);
}
} else {
break;
}
}
}
}
int ans = len.at(g.FI).at(g.SE);
if (ans == INF)
ans = -1;
COUT(ans);
// REP(i,H)COUT(len.at(i));
return 0;
}
| replace | 209 | 212 | 209 | 213 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)
#define bit(n) (1LL << (n))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end
#define ret return 0;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
lint x, y;
xy() : x(0), y(0) {}
xy(lint _x, lint _y) : x(_x), y(_y) {}
xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
bool operator<(xy p) const {
if (y == p.y)
return x < p.x;
return y < p.y;
}
};
struct xyd {
ld x, y;
xyd() : x(0), y(0) {}
xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
vec2() {}
vec2(lint n) : vector(n) {}
vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};
template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;
mt19937 mtrand((random_device())());
const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;
constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }
#if (__cplusplus < 201703L)
lint gcd(lint a, lint b) {
while (1) {
if (a < b)
swap(a, b);
if (!b)
break;
a %= b;
}
return a;
}
lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
#endif
template <class T> struct nval {
lint n; // counts
T val;
nval() : n(0){};
nval(lint _n, T _val) : n(_n), val(_val){};
};
template <class It, class It2> auto spacel(It i, It2 end) {
if (i + 1 == end) {
return '\n';
} else {
return ' ';
}
}
ostream &setp(ostream &ost) {
cout << setprecision(60) << fixed;
return ost;
}
const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;
lint h, w, k;
struct graph {
struct R {
lint to, c;
bool f;
};
lint v, e;
vector<vector<R>> gr;
graph(lint _v) : v(_v), e(0), gr(v) {}
void add_e(lint from, lint to, lint cost, bool f = false) {
gr[from].push_back({to, cost, f});
e++;
}
auto dijkstra(lint start) {
vector<lint> d(v, inf);
d[start] = 0;
struct Vc {
lint pos, d;
bool operator<(const Vc &p) const { return d > p.d; }
};
priority_queue<Vc> que;
que.push({start, 0});
while (!que.empty()) {
auto vc = que.top();
que.pop();
rep(i, gr[vc.pos].size()) {
lint nc = vc.d + gr[vc.pos][i].c;
if (gr[vc.pos][i].f)
nc = div2(nc, k) * k;
if (nc < d[gr[vc.pos][i].to]) {
d[gr[vc.pos][i].to] = nc;
que.push({gr[vc.pos][i].to, d[gr[vc.pos][i].to]});
}
}
}
return d;
}
};
lint f(lint x, lint y, lint d) { return h * w * d + w * y + x; }
template <class Tv, class Tl, class Tr> bool between(Tv val, Tl l, Tr r) {
return (val >= l) && (val <= r);
}
int main() {
cin >> h >> w >> k;
xy p1, p2;
cin >> p1.y >> p1.x >> p2.y >> p2.x;
p1.x--;
p1.y--;
p2.x--;
p2.y--;
vecs c(h);
rep(i, h) cin >> c[i];
auto is = [&](lint x, lint y) {
if (!between(x, 0, w - 1) || !between(y, 0, h - 1) || c[y][x] == '@')
return false;
return true;
};
graph g(4 * h * w);
rep(i, h) rep(j, w) {
if (is(j + 1, i)) {
g.add_e(f(j, i, 0), f(j + 1, i, 0), 1);
}
if (is(j - 1, i)) {
g.add_e(f(j, i, 1), f(j - 1, i, 1), 1);
}
if (is(j, i + 1)) {
g.add_e(f(j, i, 2), f(j, i + 1, 2), 1);
}
if (is(j, i - 1)) {
g.add_e(f(j, i, 3), f(j, i - 1, 3), 1);
}
rep(di, 4) rep(dj, 4) { g.add_e(f(j, i, di), f(j, i, dj), 0, true); }
}
lint ans = inf;
rep(i, 4) {
auto d = g.dijkstra(f(p1.x, p1.y, i));
rep(j, 4) { chmin(ans, d[f(p2.x, p2.y, j)]); }
}
if (ans == inf) {
cout << -1 << endl;
ret;
}
cout << div2(ans, k) << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define rep(i, n) for (lint i = 0; i < n; i++)
#define repr(i, n) for (lint i = n - 1; i >= 0; i--)
#define repi(i, ini, n) for (lint i = ini; i < n; i++)
#define repir(i, ini, n) for (lint i = n - 1; i >= ini; i--)
#define repb(i, start, end) for (lint i = start; i <= end; i++)
#define repbr(i, start, end) for (lint i = end; i >= start; i--)
#define bit(n) (1LL << (n))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rg(v, ini, end) v.begin() + ini, v.begin() + end
#define ret return 0;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
using lint = long long;
using ulint = unsigned long long;
using ld = long double;
struct xy {
lint x, y;
xy() : x(0), y(0) {}
xy(lint _x, lint _y) : x(_x), y(_y) {}
xy operator+(const xy &p) const { return xy(x + p.x, y + p.y); }
bool operator<(xy p) const {
if (y == p.y)
return x < p.x;
return y < p.y;
}
};
struct xyd {
ld x, y;
xyd() : x(0), y(0) {}
xyd(long double _x, long double _y) : x(_x), y(_y) {}
};
using vec = vector<lint>;
using vecd = vector<ld>;
using vecs = vector<string>;
using vecp = vector<xy>;
template <class T> using vect = vector<T>;
class vec2 : public vector<vector<lint>> {
public:
vec2() {}
vec2(lint n) : vector(n) {}
vec2(lint h, lint w) : vector(h, vector<lint>(w)) {}
vec2(lint h, lint w, lint v) : vector(h, vector<lint>(w, v)) {}
};
template <class T> using priq = priority_queue<T>;
template <class T> using rpriq = priority_queue<T, vector<T>, greater<T>>;
template <class Key, class Val> using hashmap = unordered_map<Key, Val>;
template <class Key> using hashset = unordered_set<Key>;
mt19937 mtrand((random_device())());
const double pi = 3.141592653589793238462;
const lint intmax = 9223372036854775807;
const lint inf = 1100100100100100100LL;
constexpr lint div2(lint p, lint q) { return (p + q - 1) / q; }
#if (__cplusplus < 201703L)
lint gcd(lint a, lint b) {
while (1) {
if (a < b)
swap(a, b);
if (!b)
break;
a %= b;
}
return a;
}
lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
#endif
template <class T> struct nval {
lint n; // counts
T val;
nval() : n(0){};
nval(lint _n, T _val) : n(_n), val(_val){};
};
template <class It, class It2> auto spacel(It i, It2 end) {
if (i + 1 == end) {
return '\n';
} else {
return ' ';
}
}
ostream &setp(ostream &ost) {
cout << setprecision(60) << fixed;
return ost;
}
const ulint mod = 1000000007;
// const ulint mod = 998244353;
const ld eps = 0.0000001;
lint h, w, k;
struct graph {
struct R {
lint to, c;
bool f;
};
lint v, e;
vector<vector<R>> gr;
graph(lint _v) : v(_v), e(0), gr(v) {}
void add_e(lint from, lint to, lint cost, bool f = false) {
gr[from].push_back({to, cost, f});
e++;
}
auto dijkstra(lint start) {
vector<lint> d(v, inf);
d[start] = 0;
struct Vc {
lint pos, d;
bool operator<(const Vc &p) const { return d > p.d; }
};
priority_queue<Vc> que;
que.push({start, 0});
while (!que.empty()) {
auto vc = que.top();
que.pop();
rep(i, gr[vc.pos].size()) {
lint nc = vc.d + gr[vc.pos][i].c;
if (gr[vc.pos][i].f)
nc = div2(nc, k) * k;
if (nc < d[gr[vc.pos][i].to]) {
d[gr[vc.pos][i].to] = nc;
que.push({gr[vc.pos][i].to, d[gr[vc.pos][i].to]});
}
}
}
return d;
}
};
lint f(lint x, lint y, lint d) { return h * w * d + w * y + x; }
template <class Tv, class Tl, class Tr> bool between(Tv val, Tl l, Tr r) {
return (val >= l) && (val <= r);
}
int main() {
cin >> h >> w >> k;
xy p1, p2;
cin >> p1.y >> p1.x >> p2.y >> p2.x;
p1.x--;
p1.y--;
p2.x--;
p2.y--;
vecs c(h);
rep(i, h) cin >> c[i];
auto is = [&](lint x, lint y) {
if (!between(x, 0, w - 1) || !between(y, 0, h - 1) || c[y][x] == '@')
return false;
return true;
};
graph g(4 * h * w);
rep(i, h) rep(j, w) {
if (is(j + 1, i)) {
g.add_e(f(j, i, 0), f(j + 1, i, 0), 1);
}
if (is(j - 1, i)) {
g.add_e(f(j, i, 1), f(j - 1, i, 1), 1);
}
if (is(j, i + 1)) {
g.add_e(f(j, i, 2), f(j, i + 1, 2), 1);
}
if (is(j, i - 1)) {
g.add_e(f(j, i, 3), f(j, i - 1, 3), 1);
}
rep(di, 4) rep(dj, 4) { g.add_e(f(j, i, di), f(j, i, dj), 0, true); }
}
lint ans = inf;
// rep(i, 4)
{
auto d = g.dijkstra(f(p1.x, p1.y, 0));
rep(j, 4) { chmin(ans, d[f(p2.x, p2.y, j)]); }
}
if (ans == inf) {
cout << -1 << endl;
ret;
}
cout << div2(ans, k) << endl;
return 0;
}
| replace | 222 | 224 | 222 | 225 | TLE | |
p02644 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
struct UF {
vector<int> fa;
UF(int n) {
fa.resize(n);
rep(i, n) fa[i] = i;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { fa[find(x)] = find(y); }
};
int h, w, k;
int sx, sy, ex, ey;
char temp[1 << 20];
vector<string> M;
vector<vector<int>> dis, rhv, chv;
vector<UF> ruf, cuf;
int que[1 << 20], ql, qr;
void ins(int x, int y, int d) {
dis[x][y] = d;
ruf[x].merge(y, y + 1);
cuf[y].merge(x, x + 1);
que[qr++] = x, que[qr++] = y;
}
int main() {
scanf("%d %d %d", &h, &w, &k);
scanf("%d %d %d %d", &sx, &sy, &ex, &ey), --sx, --sy, --ex, --ey;
rep(i, h) scanf("%s", temp), M.pb(temp);
rep(i, h) dis.pb(vector<int>(w, -1));
rep(i, h) rhv.pb(vector<int>(w, 0)), ruf.pb(UF(w + 1));
rep(i, w) chv.pb(vector<int>(h, 0)), cuf.pb(UF(h + 1));
rep(i, h) rep(j, w) if (M[i][j] == '@') rhv[i][j]++, chv[j][i]++;
rep(i, h) rep(j, w) if (j) rhv[i][j] += rhv[i][j - 1];
rep(i, w) rep(j, h) if (j) chv[i][j] += chv[i][j - 1];
ins(sx, sy, 0);
while (ql < qr) {
int x = que[ql++], y = que[ql++], d = dis[x][y];
{ // row
int ly = max(0, y - k);
int uy = min(w - 1, y + k);
ly = lower_bound(rhv[x].begin() + ly, rhv[x].begin() + y, rhv[x][y]) -
rhv[x].begin();
if (M[x][ly] == '@')
++ly;
uy = upper_bound(rhv[x].begin() + y + 1, rhv[x].begin() + uy + 1,
rhv[x][y]) -
rhv[x].begin() - 1;
for (int ny = ruf[x].find(ly); ny <= uy; ny = ruf[x].find(ny))
ins(x, ny, d + 1);
}
{ // col
int lx = max(0, x - k);
int ux = min(h - 1, x + k);
lx = lower_bound(chv[y].begin() + lx, chv[y].begin() + x, chv[y][x]) -
chv[y].begin();
if (M[lx][y] == '@')
++lx;
ux = upper_bound(chv[y].begin() + x + 1, chv[y].begin() + ux + 1,
chv[y][x]) -
chv[y].begin() - 1;
for (int nx = cuf[y].find(lx); nx <= ux; nx = cuf[y].find(nx))
ins(nx, y, d + 1);
}
}
printf("%d\n", dis[ex][ey]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
struct UF {
vector<int> fa;
UF(int n) {
fa.resize(n);
rep(i, n) fa[i] = i;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { fa[find(x)] = find(y); }
};
int h, w, k;
int sx, sy, ex, ey;
char temp[1 << 20];
vector<string> M;
vector<vector<int>> dis, rhv, chv;
vector<UF> ruf, cuf;
int que[1 << 21], ql, qr;
void ins(int x, int y, int d) {
dis[x][y] = d;
ruf[x].merge(y, y + 1);
cuf[y].merge(x, x + 1);
que[qr++] = x, que[qr++] = y;
}
int main() {
scanf("%d %d %d", &h, &w, &k);
scanf("%d %d %d %d", &sx, &sy, &ex, &ey), --sx, --sy, --ex, --ey;
rep(i, h) scanf("%s", temp), M.pb(temp);
rep(i, h) dis.pb(vector<int>(w, -1));
rep(i, h) rhv.pb(vector<int>(w, 0)), ruf.pb(UF(w + 1));
rep(i, w) chv.pb(vector<int>(h, 0)), cuf.pb(UF(h + 1));
rep(i, h) rep(j, w) if (M[i][j] == '@') rhv[i][j]++, chv[j][i]++;
rep(i, h) rep(j, w) if (j) rhv[i][j] += rhv[i][j - 1];
rep(i, w) rep(j, h) if (j) chv[i][j] += chv[i][j - 1];
ins(sx, sy, 0);
while (ql < qr) {
int x = que[ql++], y = que[ql++], d = dis[x][y];
{ // row
int ly = max(0, y - k);
int uy = min(w - 1, y + k);
ly = lower_bound(rhv[x].begin() + ly, rhv[x].begin() + y, rhv[x][y]) -
rhv[x].begin();
if (M[x][ly] == '@')
++ly;
uy = upper_bound(rhv[x].begin() + y + 1, rhv[x].begin() + uy + 1,
rhv[x][y]) -
rhv[x].begin() - 1;
for (int ny = ruf[x].find(ly); ny <= uy; ny = ruf[x].find(ny))
ins(x, ny, d + 1);
}
{ // col
int lx = max(0, x - k);
int ux = min(h - 1, x + k);
lx = lower_bound(chv[y].begin() + lx, chv[y].begin() + x, chv[y][x]) -
chv[y].begin();
if (M[lx][y] == '@')
++lx;
ux = upper_bound(chv[y].begin() + x + 1, chv[y].begin() + ux + 1,
chv[y][x]) -
chv[y].begin() - 1;
for (int nx = cuf[y].find(lx); nx <= ux; nx = cuf[y].find(nx))
ins(nx, y, d + 1);
}
}
printf("%d\n", dis[ex][ey]);
return 0;
}
| replace | 26 | 27 | 26 | 27 | 0 | |
p02644 | C++ | Time Limit Exceeded | // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), x.end()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int _i = 0; _i < (n); _i++) \
ni(a[_i])
#define nal(a, n) \
for (int _i = 0; _i < (n); _i++) \
nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
vector<vector<int>> ar;
vector<vector<array<int, 4>>> dp;
char t[N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ok(int x, int y) {
return 0 <= x && x < sz(ar) && 0 <= y && y < sz(ar[0]) && ar[x][y] == 0;
}
int main() {
int h, w, k;
scanf("%d %d %d", &h, &w, &k);
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1--, y1--, x2--, y2--;
ar.resize(h);
dp.resize(h);
for (int i = 0; i < h; i++) {
scanf("%s", t);
ar[i].resize(w);
dp[i].resize(w);
for (int j = 0; j < w; j++) {
ar[i][j] = t[j] == '@' ? 1 : 0;
for (int l = 0; l < 4; l++)
dp[i][j][l] = inf;
}
}
deque<pii> pq;
for (int i = 0; i < 4; i++)
dp[x1][y1][i] = 0;
pq.pb(mp(x1, y1));
while (sz(pq)) {
int x, y;
tie(x, y) = pq.front();
pq.pop_front();
int tmp = inf;
for (int i = 0; i < 4; i++)
tmp = min(tmp, dp[x][y][i]);
for (int i = 0; i < 4; i++)
dp[x][y][i] = tmp;
// cout << x << " " << y << endl;
assert(tmp != inf);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
for (int j = 1; j <= k && ok(nx, ny) && dp[nx][ny][i] >= tmp + 1; j++) {
dp[nx][ny][i] = tmp + 1;
pq.pb(mp(nx, ny));
// cout << i << " - " << j << endl;
nx += dx[i], ny += dy[i];
}
}
}
int ans = inf;
for (int l = 0; l < 4; l++)
ans = min(ans, dp[x2][y2][l]);
if (ans == inf)
ans = -1;
printf("%d\n", ans);
return 0;
}
| // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x, a) for (const auto &x : a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), x.end()
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) \
for (int _i = 0; _i < (n); _i++) \
ni(a[_i])
#define nal(a, n) \
for (int _i = 0; _i < (n); _i++) \
nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
vector<vector<int>> ar;
vector<vector<array<int, 4>>> dp;
char t[N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ok(int x, int y) {
return 0 <= x && x < sz(ar) && 0 <= y && y < sz(ar[0]) && ar[x][y] == 0;
}
int main() {
int h, w, k;
scanf("%d %d %d", &h, &w, &k);
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1--, y1--, x2--, y2--;
ar.resize(h);
dp.resize(h);
for (int i = 0; i < h; i++) {
scanf("%s", t);
ar[i].resize(w);
dp[i].resize(w);
for (int j = 0; j < w; j++) {
ar[i][j] = t[j] == '@' ? 1 : 0;
for (int l = 0; l < 4; l++)
dp[i][j][l] = inf;
}
}
deque<pii> pq;
for (int i = 0; i < 4; i++)
dp[x1][y1][i] = 0;
pq.pb(mp(x1, y1));
while (sz(pq)) {
int x, y;
tie(x, y) = pq.front();
pq.pop_front();
int tmp = inf;
for (int i = 0; i < 4; i++)
tmp = min(tmp, dp[x][y][i]);
for (int i = 0; i < 4; i++)
dp[x][y][i] = tmp;
// cout << x << " " << y << endl;
assert(tmp != inf);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
for (int j = 1; j <= k && ok(nx, ny) && dp[nx][ny][i] >= tmp + 1; j++) {
if (dp[nx][ny][i] > tmp + 1) {
dp[nx][ny][i] = tmp + 1;
pq.pb(mp(nx, ny));
}
// cout << i << " - " << j << endl;
nx += dx[i], ny += dy[i];
}
}
}
int ans = inf;
for (int l = 0; l < 4; l++)
ans = min(ans, dp[x2][y2][l]);
if (ans == inf)
ans = -1;
printf("%d\n", ans);
return 0;
}
| replace | 89 | 91 | 89 | 94 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int DIV = 998244353;
int main(int argc, char *argv[]) {
#ifdef VLAD_LOCAL
ifstream in("in.txt");
#else
istream &in = cin;
#endif
int h, w, k;
in >> h >> w >> k;
int x1, y1, x2, y2;
in >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> c(h);
for (int i = 0; i < h; i++)
in >> c[i];
vector<set<int>> waitX(h);
vector<set<int>> waitY(w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '.') {
waitX[i].insert(j);
waitY[j].insert(i);
}
}
}
vector<vector<int>> cntX(h);
for (int i = 0; i < h; i++)
cntX[i].resize(w + 1, 0);
vector<vector<int>> cntY(w);
for (int i = 0; i < w; i++)
cntY[i].resize(h + 1, 0);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
bool has = c[i][j] == '@';
cntX[i][j + 1] = cntX[i][j] + has;
cntY[j][i + 1] = cntY[j][i] + has;
}
}
vector<vector<int>> d(h);
for (int i = 0; i < h; i++)
d[i].resize(w, -1);
d[x1][y1] = 0;
waitX[x1].erase(y1);
waitY[y1].erase(x1);
priority_queue<pair<int, pair<int, int>>> q;
q.emplace(0, make_pair(x1, y1));
while (!q.empty()) {
pair<int, int> v = q.top().second;
int curD = -q.top().first;
q.pop();
vector<pair<int, int>> toDel;
set<int>::iterator itV, it;
itV = waitX[v.first].lower_bound(v.second);
for (it = itV; it != waitX[v.first].end(); it++) {
int cnt = cntX[v.first][(*it) + 1] - cntX[v.first][v.second];
int dist = (*it) - v.second;
if (cnt || dist > k)
break;
int x = v.first;
int y = *it;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
for (it = itV; it != waitX[v.first].begin();) {
it--;
int cnt = cntX[v.first][v.second + 1] - cntX[v.first][(*it)];
int dist = -(*it) + v.second;
if (cnt || dist > k)
break;
int x = v.first;
int y = *it;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
itV = waitY[v.second].lower_bound(v.first);
for (it = itV; it != waitY[v.second].end(); it++) {
int cnt = cntY[v.second][(*it) + 1] - cntY[v.second][v.first];
int dist = (*it) - v.first;
if (cnt || dist > k)
break;
int x = *it;
int y = v.second;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
for (it = itV; it != waitY[v.second].begin();) {
it--;
int cnt = cntY[v.second][v.first + 1] - cntY[v.second][(*it)];
int dist = -(*it) + v.first;
if (cnt || dist > k)
break;
int x = *it;
int y = v.second;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
}
printf("%d\n", d[x2][y2]);
return 0;
}
| #include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int DIV = 998244353;
int main(int argc, char *argv[]) {
#ifdef VLAD_LOCAL
ifstream in("in.txt");
#else
istream &in = cin;
#endif
int h, w, k;
in >> h >> w >> k;
int x1, y1, x2, y2;
in >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> c(h);
for (int i = 0; i < h; i++)
in >> c[i];
vector<set<int>> waitX(h);
vector<set<int>> waitY(w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (c[i][j] == '.') {
waitX[i].insert(j);
waitY[j].insert(i);
}
}
}
vector<vector<int>> cntX(h);
for (int i = 0; i < h; i++)
cntX[i].resize(w + 1, 0);
vector<vector<int>> cntY(w);
for (int i = 0; i < w; i++)
cntY[i].resize(h + 1, 0);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
bool has = c[i][j] == '@';
cntX[i][j + 1] = cntX[i][j] + has;
cntY[j][i + 1] = cntY[j][i] + has;
}
}
vector<vector<int>> d(h);
for (int i = 0; i < h; i++)
d[i].resize(w, -1);
d[x1][y1] = 0;
waitX[x1].erase(y1);
waitY[y1].erase(x1);
priority_queue<pair<int, pair<int, int>>> q;
q.emplace(0, make_pair(x1, y1));
while (!q.empty()) {
pair<int, int> v = q.top().second;
int curD = -q.top().first;
q.pop();
vector<pair<int, int>> toDel;
set<int>::iterator itV, it;
itV = waitX[v.first].lower_bound(v.second);
for (it = itV; it != waitX[v.first].end(); it++) {
int cnt = cntX[v.first][(*it) + 1] - cntX[v.first][v.second];
int dist = (*it) - v.second;
if (cnt || dist > k)
break;
int x = v.first;
int y = *it;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
for (it = itV; it != waitX[v.first].begin();) {
it--;
int cnt = cntX[v.first][v.second + 1] - cntX[v.first][(*it)];
int dist = -(*it) + v.second;
if (cnt || dist > k)
break;
int x = v.first;
int y = *it;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
itV = waitY[v.second].lower_bound(v.first);
for (it = itV; it != waitY[v.second].end(); it++) {
int cnt = cntY[v.second][(*it) + 1] - cntY[v.second][v.first];
int dist = (*it) - v.first;
if (cnt || dist > k)
break;
int x = *it;
int y = v.second;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
for (it = itV; it != waitY[v.second].begin();) {
it--;
int cnt = cntY[v.second][v.first + 1] - cntY[v.second][(*it)];
int dist = -(*it) + v.first;
if (cnt || dist > k)
break;
int x = *it;
int y = v.second;
if (d[x][y] < 0) {
d[x][y] = curD + 1;
q.emplace(-d[x][y], make_pair(x, y));
toDel.emplace_back(x, y);
}
}
for (int i = 0; i < toDel.size(); i++) {
waitX[toDel[i].first].erase(toDel[i].second);
waitY[toDel[i].second].erase(toDel[i].first);
}
}
printf("%d\n", d[x2][y2]);
return 0;
}
| insert | 133 | 133 | 133 | 138 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll H, W, K;
ll x1, x2, y1, y2;
std::string fld[1000010];
int main() {
std::cin >> H >> W >> K;
std::cin >> x1 >> y1 >> x2 >> y2;
--x1;
--y1;
--x2;
--y2;
std::vector<std::vector<ll>> dist(H, std::vector<ll>(W, INF));
rep(i, H) { std::cin >> fld[i]; }
dist[x1][y1] = 0;
std::queue<P> que;
que.emplace(x1, y1);
while (!que.empty()) {
P p = que.front();
que.pop();
ll i = p.first, j = p.second;
if (i == x2 && j == y2) {
std::cout << dist[i][j] << std::endl;
return 0;
}
rep(d, 4) repi(len, 1, K + 1) {
ll ni = i + dy[d] * len, nj = j + dx[d] * len;
if (!(ni >= 0 && ni < H && nj >= 0 && nj < W))
break;
if (fld[ni][nj] == '@')
break;
if (dist[ni][nj] != INF && dist[i][j] >= dist[ni][nj])
continue;
dist[ni][nj] = dist[i][j] + 1;
que.emplace(ni, nj);
}
}
std::cout << -1 << std::endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
std::cout << '(' << a.first << ", " << a.second << ')';
return out;
}
template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
std::cout << '[';
rep(i, a.size()) {
std::cout << a[i];
if (i != a.size() - 1)
std::cout << ", ";
}
std::cout << ']';
return out;
}
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll H, W, K;
ll x1, x2, y1, y2;
std::string fld[1000010];
int main() {
std::cin >> H >> W >> K;
std::cin >> x1 >> y1 >> x2 >> y2;
--x1;
--y1;
--x2;
--y2;
std::vector<std::vector<ll>> dist(H, std::vector<ll>(W, INF));
rep(i, H) { std::cin >> fld[i]; }
dist[x1][y1] = 0;
std::queue<P> que;
que.emplace(x1, y1);
while (!que.empty()) {
P p = que.front();
que.pop();
ll i = p.first, j = p.second;
if (i == x2 && j == y2) {
std::cout << dist[i][j] << std::endl;
return 0;
}
rep(d, 4) repi(len, 1, K + 1) {
ll ni = i + dy[d] * len, nj = j + dx[d] * len;
if (!(ni >= 0 && ni < H && nj >= 0 && nj < W))
break;
if (fld[ni][nj] == '@')
break;
if (dist[ni][nj] != INF && dist[i][j] >= dist[ni][nj])
break;
if (dist[ni][nj] != INF)
continue;
dist[ni][nj] = dist[i][j] + 1;
que.emplace(ni, nj);
}
}
std::cout << -1 << std::endl;
return 0;
} | insert | 103 | 103 | 103 | 106 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;
#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.first << ", " << p.second << ")";
return o;
}
#define print(v) \
{ \
cerr << #v << ": [ "; \
for (auto _ : v) \
cerr << _ << ", "; \
cerr << "]" << endl; \
}
struct Point {
int y, x;
};
int dy[4] = {-1, 0, +1, 0};
int dx[4] = {0, 1, 0, -1};
int main(void) {
int Y, X, K;
cin >> Y >> X >> K;
int sx, sy, gx, gy;
cin >> sy >> sx >> gy >> gx;
sx--, sy--, gx--, gy--;
vector<string> s(Y);
rep(y, Y) cin >> s[y];
vector<vector<int>> dp(Y, vector<int>(X, INF));
dp[sy][sx] = 0;
queue<Point> qu;
qu.push({sy, sx});
while (qu.size()) {
Point p = qu.front();
qu.pop();
rep(dir, 4) {
rep1(k, K) {
int ny = p.y + dy[dir] * k;
int nx = p.x + dx[dir] * k;
if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
break;
if (s[ny][nx] == '@')
break;
if (dp[ny][nx] <= dp[p.y][p.x])
break;
dp[ny][nx] = dp[p.y][p.x] + 1;
qu.push({ny, nx});
}
}
}
cout << ((dp[gy][gx] == INF) ? -1 : dp[gy][gx]) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;
#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
o << "(" << p.first << ", " << p.second << ")";
return o;
}
#define print(v) \
{ \
cerr << #v << ": [ "; \
for (auto _ : v) \
cerr << _ << ", "; \
cerr << "]" << endl; \
}
struct Point {
int y, x;
};
int dy[4] = {-1, 0, +1, 0};
int dx[4] = {0, 1, 0, -1};
int main(void) {
int Y, X, K;
cin >> Y >> X >> K;
int sx, sy, gx, gy;
cin >> sy >> sx >> gy >> gx;
sx--, sy--, gx--, gy--;
vector<string> s(Y);
rep(y, Y) cin >> s[y];
vector<vector<int>> dp(Y, vector<int>(X, INF));
dp[sy][sx] = 0;
queue<Point> qu;
qu.push({sy, sx});
while (qu.size()) {
Point p = qu.front();
qu.pop();
rep(dir, 4) {
rep1(k, K) {
int ny = p.y + dy[dir] * k;
int nx = p.x + dx[dir] * k;
if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
break;
if (s[ny][nx] == '@')
break;
if (dp[ny][nx] <= dp[p.y][p.x])
break;
if (dp[ny][nx] > dp[p.y][p.x] + 1) {
dp[ny][nx] = dp[p.y][p.x] + 1;
qu.push({ny, nx});
}
}
}
}
cout << ((dp[gy][gx] == INF) ? -1 : dp[gy][gx]) << endl;
return 0;
}
| replace | 59 | 61 | 59 | 63 | TLE | |
p02644 | C++ | Runtime Error | #include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
struct phase {
int x, y, d;
pair<int, int> c;
bool operator<(const phase &t) const {
if (c.first == t.c.first)
return c.second < t.c.second;
return c.first > t.c.first;
}
};
int n, m, k;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sx, sy, ex, ey;
char map[10001001];
pair<int, int> e[1001001][2];
priority_queue<phase> q;
void enque(int x, int y, int d, pair<int, int> c) {
if (e[x * m + y][d] > c) {
e[x * m + y][d] = c;
q.push({x, y, d, c});
}
}
int main() {
scanf("%d %d %d", &n, &m, &k);
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
for (int i = 0; i < n; i++)
scanf("%s", map + i * m);
for (int i = 0; i < n * m; i++)
e[i][0] = e[i][1] = {100000000, 0};
enque(sx, sy, 0, {0, 0});
enque(sx, sy, 1, {0, 0});
while (!q.empty()) {
int x = q.top().x;
int y = q.top().y;
int d = q.top().d;
auto c = q.top().c;
q.pop();
if (e[x * m + y][d] < c)
continue;
for (int f = 0; f < 4; f++) {
int px = x + dx[f];
int py = y + dy[f];
if (px < 0 || px >= n || py < 0 || py >= m || map[px * m + py] == '@')
continue;
if (c.second < 0 && f % 2 == d)
enque(px, py, f % 2, {c.first, c.second + 1});
else
enque(px, py, f % 2, {c.first + 1, 1 - k});
}
}
int ans = min(e[ex * m + ey][0].first, e[ex * m + ey][1].first);
if (ans == 100000000)
ans = -1;
printf("%d\n", ans);
return 0;
}
| #include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
struct phase {
int x, y, d;
pair<int, int> c;
bool operator<(const phase &t) const {
if (c.first == t.c.first)
return c.second < t.c.second;
return c.first > t.c.first;
}
};
int n, m, k;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sx, sy, ex, ey;
char map[10001001];
pair<int, int> e[1001001][2];
priority_queue<phase> q;
void enque(int x, int y, int d, pair<int, int> c) {
if (e[x * m + y][d] > c) {
e[x * m + y][d] = c;
q.push({x, y, d, c});
}
}
int main() {
scanf("%d %d %d", &n, &m, &k);
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
sx--;
sy--;
ex--;
ey--;
for (int i = 0; i < n; i++)
scanf("%s", map + i * m);
for (int i = 0; i < n * m; i++)
e[i][0] = e[i][1] = {100000000, 0};
enque(sx, sy, 0, {0, 0});
enque(sx, sy, 1, {0, 0});
while (!q.empty()) {
int x = q.top().x;
int y = q.top().y;
int d = q.top().d;
auto c = q.top().c;
q.pop();
if (e[x * m + y][d] < c)
continue;
for (int f = 0; f < 4; f++) {
int px = x + dx[f];
int py = y + dy[f];
if (px < 0 || px >= n || py < 0 || py >= m || map[px * m + py] == '@')
continue;
if (c.second < 0 && f % 2 == d)
enque(px, py, f % 2, {c.first, c.second + 1});
else
enque(px, py, f % 2, {c.first + 1, 1 - k});
}
}
int ans = min(e[ex * m + ey][0].first, e[ex * m + ey][1].first);
if (ans == 100000000)
ans = -1;
printf("%d\n", ans);
return 0;
}
| insert | 34 | 34 | 34 | 38 | 0 | |
p02644 | C++ | Time Limit Exceeded | // Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!
/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define all(v) (v).begin(), (v).end()
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(0);
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define vi(x) vector<x>
#define maxn 100005
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, 1, 0, -1};
ll n, m;
bool isafe(ll x, ll y) {
if (x >= 1 and x <= n and y >= 1 and y <= m)
return true;
return false;
}
signed main() {
fast;
ll k, i, j;
cin >> n >> m >> k;
char mat[n + 2][m + 2];
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++)
cin >> mat[i][j];
}
vector<vector<ll>> dj(n + 2, vector<ll>(m + 2, inf));
queue<pii> q;
q.push({x1, y1});
dj[x1][y1] = 0;
while (!q.empty()) {
pii st = q.front();
q.pop();
for (i = 0; i < 4; i++) {
for (j = 1; j <= k; j++) {
ll nx = dx[i] * j + st.fi;
ll ny = dy[i] * j + st.se;
if (isafe(nx, ny)) {
if (mat[nx][ny] == '@')
break;
if (dj[nx][ny] <= dj[st.fi][st.se] and dj[nx][ny] != inf)
continue;
if (dj[nx][ny] == inf) {
dj[nx][ny] = dj[st.fi][st.se] + 1;
q.push({nx, ny});
}
}
}
}
}
// cout<<dj[x2][y2];
cout << ((dj[x2][y2] == inf) ? -1 : dj[x2][y2]);
} | // Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!
/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define all(v) (v).begin(), (v).end()
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(0);
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define vi(x) vector<x>
#define maxn 100005
#define ordered_set \
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, 1, 0, -1};
ll n, m;
bool isafe(ll x, ll y) {
if (x >= 1 and x <= n and y >= 1 and y <= m)
return true;
return false;
}
signed main() {
fast;
ll k, i, j;
cin >> n >> m >> k;
char mat[n + 2][m + 2];
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++)
cin >> mat[i][j];
}
vector<vector<ll>> dj(n + 2, vector<ll>(m + 2, inf));
queue<pii> q;
q.push({x1, y1});
dj[x1][y1] = 0;
while (!q.empty()) {
pii st = q.front();
q.pop();
for (i = 0; i < 4; i++) {
for (j = 1; j <= k; j++) {
ll nx = dx[i] * j + st.fi;
ll ny = dy[i] * j + st.se;
if (isafe(nx, ny)) {
if (mat[nx][ny] == '@')
break;
if (dj[nx][ny] <= dj[st.fi][st.se] and dj[nx][ny] != inf)
break;
if (dj[nx][ny] == inf) {
dj[nx][ny] = dj[st.fi][st.se] + 1;
q.push({nx, ny});
}
}
}
}
}
// cout<<dj[x2][y2];
cout << ((dj[x2][y2] == inf) ? -1 : dj[x2][y2]);
} | replace | 80 | 81 | 80 | 81 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(), c.end()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <typename T> ostream &operator<<(ostream &out, const vector<T> &mas) {
for (const auto &x : mas) {
out << x << ' ';
}
return out;
}
void ok() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
inline ll nxt() {
ll x;
cin >> x;
return x;
}
int xx[] = {1, -1, 0, 0};
int yy[] = {0, 0, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n = nxt(), m = nxt(), k = nxt();
int x1 = nxt() - 1, y1 = nxt() - 1;
int x2 = nxt() - 1, y2 = nxt() - 1;
vector<vector<char>> matr(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matr[i][j];
}
}
auto is_in = [&](int x, int y) {
return (x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1);
};
auto bfs = [&]() {
queue<pii> q;
vector<vector<int>> d(n, vector<int>(m, INT_MAX));
if (matr[x1][y1] != '@') {
q.push({x1, y1});
d[x1][y1] = 0;
}
while (!q.empty()) {
pii from = q.front();
int cur = d[from.first][from.second];
q.pop();
for (int i = 0; i < 4; i++) {
int x = from.first;
int y = from.second;
for (int j = 0; j < k; j++) {
x += xx[i];
y += yy[i];
if (!is_in(x, y))
break;
if (matr[x][y] == '@') {
break;
}
if (cur + 1 > d[x][y]) {
break;
}
d[x][y] = cur + 1;
q.push({x, y});
}
}
}
return d[x2][y2];
};
int rez = bfs();
if (rez == INT_MAX)
rez = -1;
cout << rez;
return 0;
}
| #include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;*/
#define itn int
#define all(c) c.begin(), c.end()
#define make_unique(x) \
sort(all((x))); \
(x).resize(unique(all((x))) - (x).begin())
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
using namespace std;
const double PI = 3.1415926536;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <typename T> ostream &operator<<(ostream &out, const vector<T> &mas) {
for (const auto &x : mas) {
out << x << ' ';
}
return out;
}
void ok() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
inline ll nxt() {
ll x;
cin >> x;
return x;
}
int xx[] = {1, -1, 0, 0};
int yy[] = {0, 0, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n = nxt(), m = nxt(), k = nxt();
int x1 = nxt() - 1, y1 = nxt() - 1;
int x2 = nxt() - 1, y2 = nxt() - 1;
vector<vector<char>> matr(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matr[i][j];
}
}
auto is_in = [&](int x, int y) {
return (x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1);
};
auto bfs = [&]() {
queue<pii> q;
vector<vector<int>> d(n, vector<int>(m, INT_MAX));
if (matr[x1][y1] != '@') {
q.push({x1, y1});
d[x1][y1] = 0;
}
while (!q.empty()) {
pii from = q.front();
int cur = d[from.first][from.second];
q.pop();
for (int i = 0; i < 4; i++) {
int x = from.first;
int y = from.second;
for (int j = 0; j < k; j++) {
x += xx[i];
y += yy[i];
if (!is_in(x, y))
break;
if (matr[x][y] == '@') {
break;
}
if (cur + 1 > d[x][y]) {
break;
}
if (cur + 1 >= d[x][y]) {
continue;
}
d[x][y] = cur + 1;
q.push({x, y});
}
}
}
return d[x2][y2];
};
int rez = bfs();
if (rez == INT_MAX)
rez = -1;
cout << rez;
return 0;
}
| insert | 86 | 86 | 86 | 89 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define forn(i, x, n) for (int i = x; i < n; i++)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x) \
int x; \
cin >> x; \
while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void zeus() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int h, w, k;
vector<pii> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool isin(int i, int j) { return (i > 0 && j > 0 && i <= h && j <= w); }
void bfs(int x1, int y1, int x2, int y2, vector<vector<char>> pond,
vector<vi> &visited, vector<vi> &dist) {
visited[x1][y1] = 1;
dist[x1][y1] = 0;
queue<pii> q;
q.push({x1, y1});
while (!q.empty()) {
pii x = q.front();
q.pop();
// cout << x.ff << " " << x.ss << endl;
forn(i, 0, 4) {
forn(j, 1, k + 1) {
int i1 = x.ff + j * dir[i].ff;
int j1 = x.ss + j * dir[i].ss;
if (!isin(i1, j1)) {
break;
}
if (pond[i1][j1] == '@') {
break;
}
if (!visited[i1][j1]) {
visited[i1][j1] = 1;
dist[i1][j1] = 1 + dist[x.ff][x.ss];
q.push({i1, j1});
// cout << i1 << " " << j1 << endl;
}
if (i1 == x2 && j1 == y2) {
return;
}
}
}
}
}
int32_t main() {
zeus();
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<char>> pond(h + 1, vector<char>(w + 1));
vector<vector<int>> visited(h + 1, vector<int>(w + 1));
vector<vector<int>> dist(h + 1, vector<int>(w + 1));
forn(i, 1, h + 1) {
forn(j, 1, w + 1) { cin >> pond[i][j]; }
}
if (pond[x1][y1] == '@' || pond[x2][y2] == '@') {
cout << -1 << endl;
return 0;
}
bfs(x1, y1, x2, y2, pond, visited, dist);
if (visited[x2][y2] == 0) {
cout << -1 << endl;
} else {
cout << dist[x2][y2] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define forn(i, x, n) for (int i = x; i < n; i++)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x) \
int x; \
cin >> x; \
while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void zeus() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int h, w, k;
vector<pii> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool isin(int i, int j) { return (i > 0 && j > 0 && i <= h && j <= w); }
void bfs(int x1, int y1, int x2, int y2, vector<vector<char>> pond,
vector<vi> &visited, vector<vi> &dist) {
visited[x1][y1] = 1;
dist[x1][y1] = 0;
queue<pii> q;
q.push({x1, y1});
while (!q.empty()) {
pii x = q.front();
q.pop();
// cout << x.ff << " " << x.ss << endl;
forn(i, 0, 4) {
forn(j, 1, k + 1) {
int i1 = x.ff + j * dir[i].ff;
int j1 = x.ss + j * dir[i].ss;
if (!isin(i1, j1)) {
break;
}
if (pond[i1][j1] == '@') {
break;
}
if (visited[i1][j1] && dist[i1][j1] < 1 + dist[x.ff][x.ss]) {
break;
}
if (!visited[i1][j1]) {
visited[i1][j1] = 1;
dist[i1][j1] = 1 + dist[x.ff][x.ss];
q.push({i1, j1});
// cout << i1 << " " << j1 << endl;
}
if (i1 == x2 && j1 == y2) {
return;
}
}
}
}
}
int32_t main() {
zeus();
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<char>> pond(h + 1, vector<char>(w + 1));
vector<vector<int>> visited(h + 1, vector<int>(w + 1));
vector<vector<int>> dist(h + 1, vector<int>(w + 1));
forn(i, 1, h + 1) {
forn(j, 1, w + 1) { cin >> pond[i][j]; }
}
if (pond[x1][y1] == '@' || pond[x2][y2] == '@') {
cout << -1 << endl;
return 0;
}
bfs(x1, y1, x2, y2, pond, visited, dist);
if (visited[x2][y2] == 0) {
cout << -1 << endl;
} else {
cout << dist[x2][y2] << endl;
}
} | insert | 63 | 63 | 63 | 66 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
vvec<char> C(H, vec<char>(W));
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vec<set<int>> row(H), col(W);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
cin >> C[i][j];
row[i].insert(j);
col[j].insert(i);
}
for (int i = 0; i < H; i++) {
row[i].insert(-1);
row[i].insert(W);
}
for (int i = 0; i < W; i++) {
col[i].insert(-1);
col[i].insert(H);
}
int inf = 1e9;
vvec<int> dp(H, vec<int>(W, inf));
dp[sx][sy] = 0;
row[sx].erase(sy);
col[sy].erase(sx);
queue<P> Q;
Q.push({sx, sy});
auto in = [&](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; };
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
// cerr << x << " " << y << "\n";
int d = dp[x][y];
// right
auto it = row[x].upper_bound(y);
while (it != row[x].end()) {
int ny = *it;
if (!in(x, ny))
break;
if (C[x][ny] == '@')
break;
if (abs(y - ny) > K)
break;
if (dp[x][ny] == inf) {
dp[x][ny] = d + 1;
Q.push({x, ny});
col[ny].erase(x);
}
it = row[x].erase(it);
}
// left
it--;
while (it != row[x].end()) {
int ny = *it;
if (!in(x, ny))
break;
if (C[x][ny] == '@')
break;
if (abs(y - ny) > K)
break;
if (dp[x][ny] == inf) {
dp[x][ny] = d + 1;
Q.push({x, ny});
col[ny].erase(x);
}
it = row[x].erase(it);
it--;
}
// down
it = col[y].upper_bound(x);
while (it != row[y].end()) {
int nx = *it;
if (!in(nx, y))
break;
if (C[nx][y] == '@')
break;
if (abs(x - nx) > K)
break;
if (dp[nx][y] == inf) {
dp[nx][y] = d + 1;
Q.push({nx, y});
row[nx].erase(y);
}
it = col[y].erase(it);
}
// down
it--;
while (it != row[y].end()) {
int nx = *it;
if (!in(nx, y))
break;
if (C[nx][y] == '@')
break;
if (abs(x - nx) > K)
break;
if (dp[nx][y] == inf) {
dp[nx][y] = d + 1;
Q.push({nx, y});
row[nx].erase(y);
}
it = col[y].erase(it);
it--;
}
}
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cerr << dp[i][j] << (j != W - 1 ? " " : "\n");
cout << (dp[gx][gy] != inf ? dp[gx][gy] : -1) << "\n";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, K;
cin >> H >> W >> K;
vvec<char> C(H, vec<char>(W));
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vec<set<int>> row(H), col(W);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
cin >> C[i][j];
row[i].insert(j);
col[j].insert(i);
}
for (int i = 0; i < H; i++) {
row[i].insert(-1);
row[i].insert(W);
}
for (int i = 0; i < W; i++) {
col[i].insert(-1);
col[i].insert(H);
}
int inf = 1e9;
vvec<int> dp(H, vec<int>(W, inf));
dp[sx][sy] = 0;
row[sx].erase(sy);
col[sy].erase(sx);
queue<P> Q;
Q.push({sx, sy});
auto in = [&](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; };
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
// cerr << x << " " << y << "\n";
int d = dp[x][y];
// right
auto it = row[x].upper_bound(y);
while (it != row[x].end()) {
int ny = *it;
if (!in(x, ny))
break;
if (C[x][ny] == '@')
break;
if (abs(y - ny) > K)
break;
if (dp[x][ny] == inf) {
dp[x][ny] = d + 1;
Q.push({x, ny});
col[ny].erase(x);
}
it = row[x].erase(it);
}
// left
it--;
while (it != row[x].end()) {
int ny = *it;
if (!in(x, ny))
break;
if (C[x][ny] == '@')
break;
if (abs(y - ny) > K)
break;
if (dp[x][ny] == inf) {
dp[x][ny] = d + 1;
Q.push({x, ny});
col[ny].erase(x);
}
it = row[x].erase(it);
it--;
}
// down
it = col[y].upper_bound(x);
while (it != row[y].end()) {
int nx = *it;
if (!in(nx, y))
break;
if (C[nx][y] == '@')
break;
if (abs(x - nx) > K)
break;
if (dp[nx][y] == inf) {
dp[nx][y] = d + 1;
Q.push({nx, y});
row[nx].erase(y);
}
it = col[y].erase(it);
}
// down
it--;
while (it != row[y].end()) {
int nx = *it;
if (!in(nx, y))
break;
if (C[nx][y] == '@')
break;
if (abs(x - nx) > K)
break;
if (dp[nx][y] == inf) {
dp[nx][y] = d + 1;
Q.push({nx, y});
row[nx].erase(y);
}
it = col[y].erase(it);
it--;
}
}
cout << (dp[gx][gy] != inf ? dp[gx][gy] : -1) << "\n";
} | delete | 122 | 125 | 122 | 122 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template <class T> inline bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T> inline bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w, k, sx, sy, tx, ty;
cin >> h >> w >> k >> sx >> sy >> tx >> ty;
--sx, --sy, --tx, --ty;
// cerr << sx << ' ' << sy << ' ' << tx << ' ' << ty << endl;
vector<string> s(h);
for (int i = 0; i < h; ++i)
cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[sx][sy] = 0;
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> que;
que.push(T(0, sx, sy));
while (!que.empty()) {
T tup = que.top();
que.pop();
int d, x, y;
tie(d, x, y) = tup;
if (d > dist[x][y])
continue;
for (int i = 0; i < 4; ++i) {
int nx = x, ny = y;
for (int j = 0; j < k; ++j) {
nx += dx[i], ny += dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (s[nx][ny] == '@')
break;
if (chmin(dist[nx][ny], dist[x][y] + 1)) {
que.push(T(dist[nx][ny], nx, ny));
}
}
}
}
// for(int i = 0; i < h; ++i){
// for(int j = 0; j < w; ++j){
// if(dist[i][j] == INF) cerr << -1 << ' ';
// else cerr << dist[i][j] << ' ';
// }
// cerr << endl;
// }
int ans = dist[tx][ty];
if (ans == INF)
ans = -1;
cout << ans << endl;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template <class T> inline bool chmax(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T> inline bool chmin(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w, k, sx, sy, tx, ty;
cin >> h >> w >> k >> sx >> sy >> tx >> ty;
--sx, --sy, --tx, --ty;
// cerr << sx << ' ' << sy << ' ' << tx << ' ' << ty << endl;
vector<string> s(h);
for (int i = 0; i < h; ++i)
cin >> s[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
dist[sx][sy] = 0;
using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> que;
que.push(T(0, sx, sy));
while (!que.empty()) {
T tup = que.top();
que.pop();
int d, x, y;
tie(d, x, y) = tup;
if (d > dist[x][y])
continue;
for (int i = 0; i < 4; ++i) {
int nx = x, ny = y;
for (int j = 0; j < k; ++j) {
nx += dx[i], ny += dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (s[nx][ny] == '@')
break;
if (dist[nx][ny] < dist[x][y] + 1)
break;
if (chmin(dist[nx][ny], dist[x][y] + 1)) {
que.push(T(dist[nx][ny], nx, ny));
}
}
}
}
// for(int i = 0; i < h; ++i){
// for(int j = 0; j < w; ++j){
// if(dist[i][j] == INF) cerr << -1 << ' ';
// else cerr << dist[i][j] << ' ';
// }
// cerr << endl;
// }
int ans = dist[tx][ty];
if (ans == INF)
ans = -1;
cout << ans << endl;
} | insert | 82 | 82 | 82 | 84 | TLE | |
p02644 | C++ | Time Limit Exceeded | // c++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define sz(x) ((int)(x).size())
// const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL << 60;
// const int INF = INT_MAX;
const int INF = 1 << 30; // 10^9
const ll MOD = 1000000007;
// const ll MOD = 998244353;
double EPS = 1e-10;
vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};
void pres(double A) { printf("%.9lf\n", A); }
void BinarySay(ll x, ll y = 60) {
rep(i, y) cout << (x >> (y - 1 - i) & 1);
cout << endl;
}
ll cnt_bit(ll x) { return __builtin_popcountll(x); }
ll pow_long(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2)
res *= x;
x *= x;
k /= 2;
}
return res;
}
ll pow_mod(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2) {
res *= x;
res %= MOD;
}
x *= x;
x %= MOD;
k /= 2;
}
return res;
}
ll inverse(ll x) { return pow_mod(x, MOD - 2); };
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll x, ll y) {
ll res = x / gcd(x, y);
res *= y;
return res;
};
// コンビネーション
const int MAXcomb = 2000010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
// facはn!,finvは1/n!
// invは逆元
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAXcomb; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}
const int MAXkai = 500010;
ll kai_memo[MAXkai];
ll kai(ll N) {
if (kai_memo[N] != 0)
return kai_memo[N];
if (N <= 1)
return 1;
return kai_memo[N] = N * kai(N - 1) % MOD;
}
ll disit(ll s, ll base = 10) {
ll res = 0;
while (s) {
res++;
s /= base;
}
return res;
}
vector<int> divisor(int n) {
vector<int> res(0);
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
sort(res.begin(), res.end());
return res;
}
int H, W, K;
int sx, sy, tx, ty;
vector<vector<bool>> C;
vector<vector<int>> dist;
bool in(int x, int y) {
if (x >= 0 && x < H && y >= 0 && y < W)
return true;
return false;
}
void solve() {
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--, tx--, ty--;
C.resize(H);
rep(i, H) { C[i].resize(W, false); }
dist.resize(H);
rep(i, H) { dist[i].resize(W, false); }
rep(i, H) rep(j, W) {
char c;
cin >> c;
if (c == '.')
C[i][j] = true;
dist[i][j] = INF;
}
queue<pair<int, int>> Q;
dist[sx][sy] = 0;
Q.push({sx, sy});
while (Q.size()) {
pair<int, int> n = Q.front();
Q.pop();
int nx = n.f;
int ny = n.s;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
int nnx = nx + dx[i] * j;
int nny = ny + dy[i] * j;
if (!in(nnx, nny))
break;
if (!C[nnx][nny])
break;
if (dist[nnx][nny] <= dist[nx][ny])
break;
dist[nnx][nny] = dist[nx][ny] + 1;
Q.push({nnx, nny});
}
}
}
int ans = dist[tx][ty];
if (ans == INF)
ans = -1;
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
}
| // c++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define sz(x) ((int)(x).size())
// const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL << 60;
// const int INF = INT_MAX;
const int INF = 1 << 30; // 10^9
const ll MOD = 1000000007;
// const ll MOD = 998244353;
double EPS = 1e-10;
vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};
void pres(double A) { printf("%.9lf\n", A); }
void BinarySay(ll x, ll y = 60) {
rep(i, y) cout << (x >> (y - 1 - i) & 1);
cout << endl;
}
ll cnt_bit(ll x) { return __builtin_popcountll(x); }
ll pow_long(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2)
res *= x;
x *= x;
k /= 2;
}
return res;
}
ll pow_mod(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2) {
res *= x;
res %= MOD;
}
x *= x;
x %= MOD;
k /= 2;
}
return res;
}
ll inverse(ll x) { return pow_mod(x, MOD - 2); };
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll x, ll y) {
ll res = x / gcd(x, y);
res *= y;
return res;
};
// コンビネーション
const int MAXcomb = 2000010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
// facはn!,finvは1/n!
// invは逆元
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAXcomb; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll comb(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}
const int MAXkai = 500010;
ll kai_memo[MAXkai];
ll kai(ll N) {
if (kai_memo[N] != 0)
return kai_memo[N];
if (N <= 1)
return 1;
return kai_memo[N] = N * kai(N - 1) % MOD;
}
ll disit(ll s, ll base = 10) {
ll res = 0;
while (s) {
res++;
s /= base;
}
return res;
}
vector<int> divisor(int n) {
vector<int> res(0);
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i)
res.push_back(n / i);
}
}
sort(res.begin(), res.end());
return res;
}
int H, W, K;
int sx, sy, tx, ty;
vector<vector<bool>> C;
vector<vector<int>> dist;
bool in(int x, int y) {
if (x >= 0 && x < H && y >= 0 && y < W)
return true;
return false;
}
void solve() {
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--, tx--, ty--;
C.resize(H);
rep(i, H) { C[i].resize(W, false); }
dist.resize(H);
rep(i, H) { dist[i].resize(W, false); }
rep(i, H) rep(j, W) {
char c;
cin >> c;
if (c == '.')
C[i][j] = true;
dist[i][j] = INF;
}
queue<pair<int, int>> Q;
dist[sx][sy] = 0;
Q.push({sx, sy});
while (Q.size()) {
pair<int, int> n = Q.front();
Q.pop();
int nx = n.f;
int ny = n.s;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
int nnx = nx + dx[i] * j;
int nny = ny + dy[i] * j;
if (!in(nnx, nny))
break;
if (!C[nnx][nny])
break;
if (dist[nnx][nny] <= dist[nx][ny])
break;
if (dist[nnx][nny] > dist[nx][ny] + 1) {
dist[nnx][nny] = dist[nx][ny] + 1;
Q.push({nnx, nny});
}
}
}
}
int ans = dist[tx][ty];
if (ans == INF)
ans = -1;
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
}
| replace | 190 | 192 | 190 | 194 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
signed main() {
int h, w, k;
cin >> h >> w >> k;
int xs, ys, xt, yt;
cin >> ys >> xs >> yt >> xt;
--xs, --xt, --ys, --yt;
char c[h][w];
rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
int dist[h][w];
rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
queue<pint> q;
q.push({ys, xs});
dist[ys][xs] = 0;
while (!q.empty()) {
pint now = q.front();
q.pop();
int ny = now.first, nx = now.second;
rep(i, 0, 4) {
rep(j, 1, k + 1) {
int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
break;
if (c[nexty][nextx] == '@')
break;
if (dist[nexty][nextx] <= dist[ny][nx])
break;
dist[nexty][nextx] = dist[ny][nx] + 1;
q.push({nexty, nextx});
}
}
}
/*
rep (i, 0, h) {
rep (j, 0, w) cout << dist[i][j] << " ";
cout << "\n";
}
*/
if (dist[yt][xt] == inf) {
cout << "-1\n";
} else {
cout << dist[yt][xt] << "\n";
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
signed main() {
int h, w, k;
cin >> h >> w >> k;
int xs, ys, xt, yt;
cin >> ys >> xs >> yt >> xt;
--xs, --xt, --ys, --yt;
char c[h][w];
rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
int dist[h][w];
rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
queue<pint> q;
q.push({ys, xs});
dist[ys][xs] = 0;
while (!q.empty()) {
pint now = q.front();
q.pop();
int ny = now.first, nx = now.second;
rep(i, 0, 4) {
rep(j, 1, k + 1) {
int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
break;
if (c[nexty][nextx] == '@')
break;
if (dist[nexty][nextx] <= dist[ny][nx])
break;
if (dist[nexty][nextx] > dist[ny][nx] + 1) {
dist[nexty][nextx] = dist[ny][nx] + 1;
q.push({nexty, nextx});
}
}
}
}
/*
rep (i, 0, h) {
rep (j, 0, w) cout << dist[i][j] << " ";
cout << "\n";
}
*/
if (dist[yt][xt] == inf) {
cout << "-1\n";
} else {
cout << dist[yt][xt] << "\n";
}
} | replace | 36 | 38 | 36 | 40 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");
const int h = 1000000007;
signed main() {
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cout.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> grid(h);
cin >> grid;
vector<vvi> vis(4, vvi(h, vi(w)));
queue<pii> q;
q.push({x1, y1});
vvi vis_time(h, vi(w, INT_MAX));
vis_time[x1][y1] = 0;
vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!q.empty()) {
pii cur = q.front();
q.pop();
rep(i, 0, 4) vis[i][cur.F][cur.S] = true;
rep(i, 0, 4) {
pii nex = cur;
rep(_, 0, k) {
nex = {nex.F + dir[i].F, nex.S + dir[i].S};
if (nex.F < 0 or nex.F == h or nex.S < 0 or nex.S == w)
break;
if (grid[nex.F][nex.S] == '@' or
vis_time[nex.F][nex.S] ==
vis_time[cur.F][cur.S]) // or vis[i][nex.F][nex.S])
break;
vis[i][nex.F][nex.S] = true;
if (vis_time[nex.F][nex.S] == INT_MAX)
vis_time[nex.F][nex.S] = vis_time[cur.F][cur.S] + 1;
q.push(nex);
}
}
}
cout << (vis_time[x2][y2] == INT_MAX ? -1 : vis_time[x2][y2]);
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");
const int h = 1000000007;
signed main() {
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cout.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> grid(h);
cin >> grid;
vector<vvi> vis(4, vvi(h, vi(w)));
queue<pii> q;
q.push({x1, y1});
vvi vis_time(h, vi(w, INT_MAX));
vis_time[x1][y1] = 0;
vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!q.empty()) {
pii cur = q.front();
q.pop();
rep(i, 0, 4) vis[i][cur.F][cur.S] = true;
rep(i, 0, 4) {
pii nex = cur;
rep(_, 0, k) {
nex = {nex.F + dir[i].F, nex.S + dir[i].S};
if (nex.F < 0 or nex.F == h or nex.S < 0 or nex.S == w)
break;
if (grid[nex.F][nex.S] == '@' or
vis_time[nex.F][nex.S] == vis_time[cur.F][cur.S] or
vis[i][nex.F][nex.S])
break;
vis[i][nex.F][nex.S] = true;
if (vis_time[nex.F][nex.S] == INT_MAX)
vis_time[nex.F][nex.S] = vis_time[cur.F][cur.S] + 1;
q.push(nex);
}
}
}
cout << (vis_time[x2][y2] == INT_MAX ? -1 : vis_time[x2][y2]);
}
| replace | 117 | 119 | 117 | 119 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");
const int h = 1000000007;
signed main() {
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cout.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> grid(h);
cin >> grid;
vector<vvi> vis(4, vvi(h, vi(w)));
queue<pii> q;
q.push({x1, y1});
vvi vis_time(h, vi(w, INT_MAX));
vis_time[x1][y1] = 0;
vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!q.empty()) {
pii cur = q.front();
q.pop();
rep(i, 0, 4) vis[i][cur.F][cur.S] = true;
rep(i, 0, 4) {
pii nex = cur;
rep(_, 0, k) {
nex = {nex.F + dir[i].F, nex.S + dir[i].S};
if (nex.F < 0 or nex.F == h or nex.S < 0 or nex.S == w)
break;
if (grid[nex.F][nex.S] == '@' or
vis_time[nex.F][nex.S] <= vis_time[cur.F][cur.S])
break;
vis[i][nex.F][nex.S] = true;
if (vis_time[nex.F][nex.S] == INT_MAX)
vis_time[nex.F][nex.S] = vis_time[cur.F][cur.S] + 1;
q.push(nex);
}
}
}
cout << (vis_time[x2][y2] == INT_MAX ? -1 : vis_time[x2][y2]);
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
out << '(' << p.first << ',' << p.second << ')';
return out;
}
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd(long long a, long long b) {
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");
const int h = 1000000007;
signed main() {
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cout.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> grid(h);
cin >> grid;
vector<vvi> vis(4, vvi(h, vi(w)));
queue<pii> q;
q.push({x1, y1});
vvi vis_time(h, vi(w, INT_MAX));
vis_time[x1][y1] = 0;
vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!q.empty()) {
pii cur = q.front();
q.pop();
rep(i, 0, 4) vis[i][cur.F][cur.S] = true;
rep(i, 0, 4) {
pii nex = cur;
rep(_, 0, k) {
nex = {nex.F + dir[i].F, nex.S + dir[i].S};
if (nex.F < 0 or nex.F == h or nex.S < 0 or nex.S == w)
break;
if (grid[nex.F][nex.S] == '@' or
vis_time[nex.F][nex.S] <= vis_time[cur.F][cur.S] or
vis[i][nex.F][nex.S])
break;
vis[i][nex.F][nex.S] = true;
if (vis_time[nex.F][nex.S] == INT_MAX)
vis_time[nex.F][nex.S] = vis_time[cur.F][cur.S] + 1;
q.push(nex);
}
}
}
cout << (vis_time[x2][y2] == INT_MAX ? -1 : vis_time[x2][y2]);
}
| replace | 117 | 118 | 117 | 119 | TLE | |
p02644 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 200002;
#define endl '\n'
#define int ll
#define ld long double
// #define all(x) (x).begin(),(x).end()
queue<int> q;
bool vis[2001001];
int dist[2001010];
string s[2001001];
int n, m, K;
int x1, Y1, x2, y2;
int get(int i, int j) { return (i * m + j); }
set<int> col[1000001], row[1000001];
int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
bool isvalid(int i, int j) {
return (i >= 0 and j >= 0 and i < n and j < m and s[i][j] != '@');
}
void bfs() {
q.push(get(x1, Y1));
dist[get(x1, Y1)] = 0;
row[x1].erase(Y1);
col[Y1].erase(x1);
int cnt = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
int i = u / m;
int j = u % m;
cnt++;
assert(cnt < 100000000);
int cur = dist[get(i, j)];
if (!isvalid(i, j))
continue;
for (int k = 0; k < 4; k++) {
int ni = i + di[k];
int nj = j + dj[k];
if (!isvalid(ni, nj))
continue;
if (di[k] == 1) {
auto it = col[nj].lower_bound(i);
while (it != col[nj].end() and abs(*it - i) <= K) {
int val = *it;
if (!isvalid(val, nj))
break;
dist[get(val, nj)] = cur + 1;
col[nj].erase(val);
row[val].erase(nj);
q.push(get(val, nj));
it = col[nj].lower_bound(i);
}
}
if (di[k] == -1) {
auto it = col[nj].lower_bound(i);
while (it != col[nj].begin() and abs(*(--it) - i) <= K) {
int val = *it;
if (!isvalid(val, nj))
break;
dist[get(val, nj)] = cur + 1;
col[nj].erase(val);
row[val].erase(nj);
q.push(get(val, nj));
it = col[nj].lower_bound(i);
}
}
if (dj[k] == 1) {
auto it = row[ni].lower_bound(j);
while (it != row[ni].end() and abs(*it - j) <= K) {
int val = *it;
if (!isvalid(ni, val))
break;
dist[get(ni, val)] = cur + 1;
row[ni].erase(val);
col[val].erase(ni);
q.push(get(ni, val));
it = row[ni].lower_bound(j);
}
}
if (dj[k] == -1) {
auto it = row[ni].lower_bound(j);
while (it != row[ni].begin() and abs(*(--it) - j) <= K) {
int val = *it;
if (!isvalid(ni, val))
break;
dist[get(ni, val)] = cur + 1;
row[ni].erase(val);
col[val].erase(ni);
q.push(get(ni, val));
it = row[ni].lower_bound(j);
}
}
}
}
}
int32_t main() {
IOS
cin >>
n >> m >> K;
cin >> x1 >> Y1 >> x2 >> y2;
x1--;
Y1--;
x2--;
y2--;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
row[i].insert(j);
col[j].insert(i);
}
}
for (int i = 0; i < (n + 2) * (m + 2); i++)
dist[i] = 1e9;
bfs();
if (dist[get(x2, y2)] > 1e7) {
cout << -1 << endl;
} else
cout << dist[get(x2, y2)];
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 200002;
#define endl '\n'
#define int ll
#define ld long double
// #define all(x) (x).begin(),(x).end()
queue<int> q;
bool vis[2001001];
int dist[2001010];
string s[2001001];
int n, m, K;
int x1, Y1, x2, y2;
int get(int i, int j) { return (i * m + j); }
set<int> col[1000001], row[1000001];
int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
bool isvalid(int i, int j) {
return (i >= 0 and j >= 0 and i < n and j < m and s[i][j] != '@');
}
void bfs() {
q.push(get(x1, Y1));
dist[get(x1, Y1)] = 0;
row[x1].erase(Y1);
col[Y1].erase(x1);
int cnt = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
int i = u / m;
int j = u % m;
cnt++;
assert(cnt < 100000000);
int cur = dist[get(i, j)];
if (!isvalid(i, j))
continue;
for (int k = 0; k < 4; k++) {
int ni = i + di[k];
int nj = j + dj[k];
if (!isvalid(ni, nj))
continue;
if (di[k] == 1) {
auto it = col[nj].lower_bound(i);
while (it != col[nj].end() and abs(*it - i) <= K) {
int val = *it;
if (!isvalid(val, nj))
break;
dist[get(val, nj)] = cur + 1;
col[nj].erase(val);
row[val].erase(nj);
q.push(get(val, nj));
it = col[nj].lower_bound(i);
}
}
if (di[k] == -1) {
auto it = col[nj].lower_bound(i);
while (it != col[nj].begin() and abs(*(--it) - i) <= K) {
int val = *it;
if (!isvalid(val, nj))
break;
dist[get(val, nj)] = cur + 1;
col[nj].erase(val);
row[val].erase(nj);
q.push(get(val, nj));
it = col[nj].lower_bound(i);
}
}
if (dj[k] == 1) {
auto it = row[ni].lower_bound(j);
while (it != row[ni].end() and abs(*it - j) <= K) {
int val = *it;
if (!isvalid(ni, val))
break;
dist[get(ni, val)] = cur + 1;
row[ni].erase(val);
col[val].erase(ni);
q.push(get(ni, val));
it = row[ni].lower_bound(j);
}
}
if (dj[k] == -1) {
auto it = row[ni].lower_bound(j);
while (it != row[ni].begin() and abs(*(--it) - j) <= K) {
int val = *it;
if (!isvalid(ni, val))
break;
dist[get(ni, val)] = cur + 1;
row[ni].erase(val);
col[val].erase(ni);
q.push(get(ni, val));
it = row[ni].lower_bound(j);
}
}
}
}
}
int32_t main() {
IOS
cin >>
n >> m >> K;
cin >> x1 >> Y1 >> x2 >> y2;
x1--;
Y1--;
x2--;
y2--;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
row[i].insert(j);
col[j].insert(i);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
dist[get(i, j)] = 1e9;
}
bfs();
if (dist[get(x2, y2)] > 1e7) {
cout << -1 << endl;
} else
cout << dist[get(x2, y2)];
} | replace | 159 | 161 | 159 | 163 | -11 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
for (auto el : p)
cout << "[" << el.first << ";" << el.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int H, W, K;
vector<string> pond;
int sx, sy, tx, ty;
void input() {
fast_io();
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--;
tx--, ty--;
for (int i = 0; i < H; i++) {
string s;
cin >> s;
pond.push_back(s);
}
}
#define x first
#define y second
int solve() {
queue<pair<int, pii>> q;
q.emplace(0, make_pair(sx, sy));
vector<vector<int>> dist(H, vector<int>(W, INT_INF));
dist[sx][sy] = 0;
map<pii, pii> pref;
while (!q.empty()) {
int d = q.front().first;
pii cur = q.front().second;
q.pop();
if (dist[cur.x][cur.y] < d)
continue;
dist[cur.x][cur.y] = d;
for (int k = 0; k < 4; k++) {
for (int t = 1; t <= K; t++) {
pii next = cur;
next.x += t * dx[k];
next.y += t * dy[k];
if (next.x < 0 || next.x >= H)
break;
if (next.y < 0 || next.y >= W)
break;
if (pond[next.x][next.y] == '@')
break;
if (dist[next.x][next.y] <= dist[cur.x][cur.y])
break;
// if(dist[next.x][next.y]<=d+1) continue;
dist[next.x][next.y] = d + 1;
pref[next] = cur;
if (next.x == tx && next.y == ty)
break;
q.emplace(d + 1, next);
}
}
}
if (dist[tx][ty] == INT_INF) {
cout << -1 << endl;
return 0;
} else
cout << dist[tx][ty] << endl;
return 0;
}
#undef x
#undef y
int main() {
input();
solve();
return 0;
} | #include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
for (auto el : p)
cout << "[" << el.first << ";" << el.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int H, W, K;
vector<string> pond;
int sx, sy, tx, ty;
void input() {
fast_io();
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--;
tx--, ty--;
for (int i = 0; i < H; i++) {
string s;
cin >> s;
pond.push_back(s);
}
}
#define x first
#define y second
int solve() {
queue<pair<int, pii>> q;
q.emplace(0, make_pair(sx, sy));
vector<vector<int>> dist(H, vector<int>(W, INT_INF));
dist[sx][sy] = 0;
map<pii, pii> pref;
while (!q.empty()) {
int d = q.front().first;
pii cur = q.front().second;
q.pop();
if (dist[cur.x][cur.y] < d)
continue;
dist[cur.x][cur.y] = d;
for (int k = 0; k < 4; k++) {
for (int t = 1; t <= K; t++) {
pii next = cur;
next.x += t * dx[k];
next.y += t * dy[k];
if (next.x < 0 || next.x >= H)
break;
if (next.y < 0 || next.y >= W)
break;
if (pond[next.x][next.y] == '@')
break;
if (dist[next.x][next.y] <= dist[cur.x][cur.y])
break;
if (dist[next.x][next.y] <= d + 1)
continue;
dist[next.x][next.y] = d + 1;
pref[next] = cur;
if (next.x == tx && next.y == ty)
break;
q.emplace(d + 1, next);
}
}
}
if (dist[tx][ty] == INT_INF) {
cout << -1 << endl;
return 0;
} else
cout << dist[tx][ty] << endl;
return 0;
}
#undef x
#undef y
int main() {
input();
solve();
return 0;
} | replace | 107 | 108 | 107 | 109 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
for (auto el : p)
cout << "[" << el.first << ";" << el.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int H, W, K;
vector<string> pond;
int sx, sy, tx, ty;
void input() {
fast_io();
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--;
tx--, ty--;
for (int i = 0; i < H; i++) {
string s;
cin >> s;
pond.push_back(s);
}
}
#define x first
#define y second
int solve() {
queue<pair<int, pii>> q;
q.emplace(0, make_pair(sx, sy));
vector<vector<int>> dist(H, vector<int>(W, INT_INF));
dist[sx][sy] = 0;
map<pii, pii> pref;
while (!q.empty()) {
int d = q.front().first;
pii cur = q.front().second;
q.pop();
if (dist[cur.x][cur.y] < d)
continue;
for (int k = 0; k < 4; k++) {
for (int t = 1; t <= K; t++) {
pii next = cur;
next.x += t * dx[k];
next.y += t * dy[k];
if (next.x < 0 || next.x >= H)
break;
if (next.y < 0 || next.y >= W)
break;
if (pond[next.x][next.y] == '@')
break;
if (dist[next.x][next.y] <= dist[cur.x][cur.y])
break;
dist[next.x][next.y] = d + 1;
pref[next] = cur;
if (next.x == tx && next.y == ty)
break;
q.emplace(d + 1, next);
}
}
}
if (dist[tx][ty] == INT_INF) {
cout << -1 << endl;
return 0;
} else
cout << dist[tx][ty] << endl;
return 0;
}
#undef x
#undef y
int main() {
input();
solve();
return 0;
} | #include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
for (auto el : p)
cout << "[" << el.first << ";" << el.second << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
for (auto el : v)
cout << el << " ";
return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
vector<T> ret(sz);
for (auto &elem : ret)
cin >> elem;
return ret;
}
int H, W, K;
vector<string> pond;
int sx, sy, tx, ty;
void input() {
fast_io();
cin >> H >> W >> K;
cin >> sx >> sy >> tx >> ty;
sx--, sy--;
tx--, ty--;
for (int i = 0; i < H; i++) {
string s;
cin >> s;
pond.push_back(s);
}
}
#define x first
#define y second
int solve() {
queue<pair<int, pii>> q;
q.emplace(0, make_pair(sx, sy));
vector<vector<int>> dist(H, vector<int>(W, INT_INF));
dist[sx][sy] = 0;
map<pii, pii> pref;
while (!q.empty()) {
int d = q.front().first;
pii cur = q.front().second;
q.pop();
if (dist[cur.x][cur.y] < d)
continue;
for (int k = 0; k < 4; k++) {
for (int t = 1; t <= K; t++) {
pii next = cur;
next.x += t * dx[k];
next.y += t * dy[k];
if (next.x < 0 || next.x >= H)
break;
if (next.y < 0 || next.y >= W)
break;
if (pond[next.x][next.y] == '@')
break;
if (dist[next.x][next.y] <= dist[cur.x][cur.y])
break;
if (dist[next.x][next.y] <= d + 1)
continue;
dist[next.x][next.y] = d + 1;
pref[next] = cur;
if (next.x == tx && next.y == ty)
break;
q.emplace(d + 1, next);
}
}
}
if (dist[tx][ty] == INT_INF) {
cout << -1 << endl;
return 0;
} else
cout << dist[tx][ty] << endl;
return 0;
}
#undef x
#undef y
int main() {
input();
solve();
return 0;
} | insert | 106 | 106 | 106 | 108 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\n'
#define M 1000000007
#define int long long
#define INF 1e18
#define max_n 1000005
using namespace std;
ll h, w, k;
ll x1, yy1, x2, y2;
string a[max_n];
vector<vector<ll>> ans;
vector<vector<bool>> vis;
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
void bfs(ll x, ll y) {
ans[x][y] = 0;
vis[x][y] = true;
queue<pair<ll, ll>> q;
q.push({x, y});
while (!q.empty()) {
ll xx = q.front().first, yy = q.front().second;
// cout<<xx<<" "<<yy<<endl;
q.pop();
for (int j = 0; j < 4; ++j) {
for (int i = 1; i <= k; ++i) {
ll nx = xx + dx[j] * i;
ll ny = yy + dy[j] * i;
if (nx < 0 || ny < 0 || nx >= h || ny >= w || a[nx][ny] == '@')
break;
if (vis[nx][ny])
continue;
vis[nx][ny] = true;
ans[nx][ny] = ans[xx][yy] + 1;
// deb(ans[nx][ny])
q.push({nx, ny});
}
}
}
}
void solve() {
cin >> h >> w >> k;
cin >> x1 >> yy1 >> x2 >> y2;
for (int i = 0; i < h; ++i) {
cin >> a[i];
}
x1--;
yy1--;
x2--;
y2--;
ans = vector<vector<ll>>(h, vector<ll>(w, -1LL));
vis = vector<vector<bool>>(h, vector<bool>(w, false));
bfs(x1, yy1);
cout << ans[x2][y2] << endl;
}
int32_t main() {
IOS ll T = 1;
// cin>>T;
for (ll i = 1; i <= T; ++i) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\n'
#define M 1000000007
#define int long long
#define INF 1e18
#define max_n 1000005
using namespace std;
ll h, w, k;
ll x1, yy1, x2, y2;
string a[max_n];
vector<vector<ll>> ans;
vector<vector<bool>> vis;
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
void bfs(ll x, ll y) {
ans[x][y] = 0;
vis[x][y] = true;
queue<pair<ll, ll>> q;
q.push({x, y});
while (!q.empty()) {
ll xx = q.front().first, yy = q.front().second;
// cout<<xx<<" "<<yy<<endl;
q.pop();
for (int j = 0; j < 4; ++j) {
for (int i = 1; i <= k; ++i) {
ll nx = xx + dx[j] * i;
ll ny = yy + dy[j] * i;
if (nx < 0 || ny < 0 || nx >= h || ny >= w || a[nx][ny] == '@')
break;
if (vis[nx][ny] && ans[nx][ny] < ans[xx][yy] + 1LL)
break;
if (vis[nx][ny])
continue;
vis[nx][ny] = true;
ans[nx][ny] = ans[xx][yy] + 1;
// deb(ans[nx][ny])
q.push({nx, ny});
}
}
}
}
void solve() {
cin >> h >> w >> k;
cin >> x1 >> yy1 >> x2 >> y2;
for (int i = 0; i < h; ++i) {
cin >> a[i];
}
x1--;
yy1--;
x2--;
y2--;
ans = vector<vector<ll>>(h, vector<ll>(w, -1LL));
vis = vector<vector<bool>>(h, vector<bool>(w, false));
bfs(x1, yy1);
cout << ans[x2][y2] << endl;
}
int32_t main() {
IOS ll T = 1;
// cin>>T;
for (ll i = 1; i <= T; ++i) {
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | insert | 38 | 38 | 38 | 40 | TLE | |
p02644 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;
int main() {
freopen("killer_01.txt", "r", stdin);
freopen("killer_01_out.txt", "w", stdout);
cin >> H >> W >> K;
cin >> st_x >> st_y >> end_x >> end_y;
--st_x, --st_y, --end_x, --end_y;
vector<vector<char>> g(H, vector<char>(W));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> g[i][j];
vector<vector<int>> dist(H, vector<int>(W, INF));
dist[st_x][st_y] = 0;
queue<pii> q;
vector<int> dir1 = {-1, 1, 0, 0};
vector<int> dir2 = {0, 0, 1, -1};
q.push({st_x, st_y});
while (!q.empty()) {
auto f = q.front();
q.pop();
int x = f.F, y = f.S, d = dist[x][y];
if (x == end_x && y == end_y) {
cout << d << endl;
return 0;
}
for (int pos = 0; pos < 4; pos++)
for (int i = 1; i <= K; i++) {
int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
g[xx][yy] == '@')
break;
if (dist[xx][yy] > d + 1) {
dist[xx][yy] = d + 1;
q.push({xx, yy});
}
}
}
cout << -1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;
int main() {
// freopen("killer_01.txt" , "r" , stdin);
// freopen("killer_01_out.txt" , "w" , stdout);
cin >> H >> W >> K;
cin >> st_x >> st_y >> end_x >> end_y;
--st_x, --st_y, --end_x, --end_y;
vector<vector<char>> g(H, vector<char>(W));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> g[i][j];
vector<vector<int>> dist(H, vector<int>(W, INF));
dist[st_x][st_y] = 0;
queue<pii> q;
vector<int> dir1 = {-1, 1, 0, 0};
vector<int> dir2 = {0, 0, 1, -1};
q.push({st_x, st_y});
while (!q.empty()) {
auto f = q.front();
q.pop();
int x = f.F, y = f.S, d = dist[x][y];
if (x == end_x && y == end_y) {
cout << d << endl;
return 0;
}
for (int pos = 0; pos < 4; pos++)
for (int i = 1; i <= K; i++) {
int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
g[xx][yy] == '@')
break;
if (dist[xx][yy] > d + 1) {
dist[xx][yy] = d + 1;
q.push({xx, yy});
}
}
}
cout << -1 << endl;
return 0;
} | replace | 9 | 11 | 9 | 11 | -11 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
class state {
public:
int x, y, d;
ll c;
state() {}
state(int xx, int yy, int dd, ll cc) {
x = xx;
y = yy;
d = dd;
c = cc;
}
bool operator<(const state &s) const { return c < s.c; }
};
int h, w, k;
int sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> fie[1000006];
vector<ll> dp[4][1000006];
int main(void) {
scanf("%d%d%d", &h, &w, &k);
scanf("%d%d%d%d", &sy, &sx, &gy, &gx);
sx--;
sy--;
gx--;
gy--;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < w; j++) {
for (int k = 0; k < 4; k++) {
dp[k][i].push_back(1LL << 60);
}
if (s[j] == '.') {
fie[i].push_back(0);
} else {
fie[i].push_back(-1);
}
}
}
dp[0][sy][sx] = 0;
priority_queue<state> que;
que.push(state(sx, sy, 0, 0));
while (que.size()) {
state st = que.top();
que.pop();
if (dp[st.d][st.y][st.x] < st.c) {
continue;
}
// printf("%d %d %d %lld\n",st.x,st.y,st.d,st.c);
for (int j = 0; j < 4; j++) {
int nx = st.x + dx[j];
int ny = st.y + dy[j];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (fie[ny][nx] == -1)
continue;
ll nc = st.c + 1;
if (st.d != j) {
if (st.c % k == 0) {
nc = st.c + 1;
} else {
nc = st.c + k - (st.c % k) + 1;
}
}
if (dp[j][ny][nx] > nc) {
dp[j][ny][nx] = nc;
que.push(state(nx, ny, j, nc));
}
}
}
ll ans = 1LL << 60;
for (int i = 0; i < 4; i++) {
ans = min(ans, dp[i][gy][gx]);
}
printf("%lld\n", ans == (1LL << 60) ? -1 : (ans + k - 1) / k);
return 0;
}
| #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
class state {
public:
int x, y, d;
ll c;
state() {}
state(int xx, int yy, int dd, ll cc) {
x = xx;
y = yy;
d = dd;
c = cc;
}
bool operator<(const state &s) const { return c > s.c; }
};
int h, w, k;
int sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<int> fie[1000006];
vector<ll> dp[4][1000006];
int main(void) {
scanf("%d%d%d", &h, &w, &k);
scanf("%d%d%d%d", &sy, &sx, &gy, &gx);
sx--;
sy--;
gx--;
gy--;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < w; j++) {
for (int k = 0; k < 4; k++) {
dp[k][i].push_back(1LL << 60);
}
if (s[j] == '.') {
fie[i].push_back(0);
} else {
fie[i].push_back(-1);
}
}
}
dp[0][sy][sx] = 0;
priority_queue<state> que;
que.push(state(sx, sy, 0, 0));
while (que.size()) {
state st = que.top();
que.pop();
if (dp[st.d][st.y][st.x] < st.c) {
continue;
}
// printf("%d %d %d %lld\n",st.x,st.y,st.d,st.c);
for (int j = 0; j < 4; j++) {
int nx = st.x + dx[j];
int ny = st.y + dy[j];
if (nx < 0 || nx >= w || ny < 0 || ny >= h)
continue;
if (fie[ny][nx] == -1)
continue;
ll nc = st.c + 1;
if (st.d != j) {
if (st.c % k == 0) {
nc = st.c + 1;
} else {
nc = st.c + k - (st.c % k) + 1;
}
}
if (dp[j][ny][nx] > nc) {
dp[j][ny][nx] = nc;
que.push(state(nx, ny, j, nc));
}
}
}
ll ans = 1LL << 60;
for (int i = 0; i < 4; i++) {
ans = min(ans, dp[i][gy][gx]);
}
printf("%lld\n", ans == (1LL << 60) ? -1 : (ans + k - 1) / k);
return 0;
}
| replace | 17 | 18 | 17 | 18 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
/* start */
const int inf = 1e8;
vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
signed main() {
IOS;
int h, w, k;
int x1, y1, x2, y2;
cin >> h >> w >> k;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> g(h);
for (auto &it : g)
cin >> it;
vector<vector<int>> d(h, vector<int>(w, inf));
d[x1][y1] = 0;
priority_queue<pair<int, pair<int, int>>> q;
q.push(make_pair(0, make_pair(x1, y1)));
while (!q.empty()) {
pair<int, int> now = q.top().second;
int dist = -q.top().first;
q.pop();
if (dist > d[now.first][now.second])
continue;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int x = now.first + dx[i] * j;
int y = now.second + dy[i] * j;
if (x >= 0 and x < h and y >= 0 and y < w) {
if (g[x][y] == '@')
break;
if (dist + 1 < d[x][y]) {
d[x][y] = dist + 1;
q.push(make_pair(-(dist + 1), make_pair(x, y)));
}
} else
break;
}
}
}
cout << ((d[x2][y2] == inf) ? -1 : d[x2][y2]) << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(0)
/* start */
const int inf = 1e8;
vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
signed main() {
IOS;
int h, w, k;
int x1, y1, x2, y2;
cin >> h >> w >> k;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> g(h);
for (auto &it : g)
cin >> it;
vector<vector<int>> d(h, vector<int>(w, inf));
d[x1][y1] = 0;
priority_queue<pair<int, pair<int, int>>> q;
q.push(make_pair(0, make_pair(x1, y1)));
while (!q.empty()) {
pair<int, int> now = q.top().second;
int dist = -q.top().first;
q.pop();
if (dist > d[now.first][now.second])
continue;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= k; j++) {
int x = now.first + dx[i] * j;
int y = now.second + dy[i] * j;
if (x >= 0 and x < h and y >= 0 and y < w) {
if (g[x][y] == '@')
break;
if (dist + 1 > d[x][y])
break;
if (dist + 1 < d[x][y]) {
d[x][y] = dist + 1;
q.push(make_pair(-(dist + 1), make_pair(x, y)));
}
} else
break;
}
}
}
cout << ((d[x2][y2] == inf) ? -1 : d[x2][y2]) << "\n";
} | insert | 39 | 39 | 39 | 41 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v) \
for (auto &z : v) \
cout << z << ' '; \
cout << '\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii mov[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
int n, i, j, t;
boost;
int r, c, k;
cin >> r >> c >> k;
pii s, f;
cin >> s.F >> s.S >> f.F >> f.S;
s.F--;
s.S--;
f.F--;
f.S--;
bool arr[r][c];
clean(arr);
rep(i, 0, r) {
string s;
cin >> s;
rep(j, 0, c) if (s[j] == '@') arr[i][j] = 1;
}
// bool vis[r][c];
// clean(vis);
int best[r][c];
rep(i, 0, r) { rep(j, 0, c) best[i][j] = 1e9; }
priority_queue<pair<int, pii>> q;
pair<int, pii> u;
int x, y;
q.push({0, s});
// vis[s.F][s.S]=1;
best[s.F][s.S] = 0;
while (!q.empty()) {
u = q.top();
// cout<<u.F<<' '<<u.S<<'\n';
q.pop();
if (-u.F > best[u.S.F][u.S.S])
continue;
for (auto &z : mov) {
j = k;
x = u.S.F;
y = u.S.S;
while (j--) {
x += z.F;
y += z.S;
if (!(x >= 0 and y >= 0 and x < r and y < c and !arr[x][y] and
best[x][y] >= 1 - u.F))
break;
best[x][y] = 1 - u.F;
q.push({-best[x][y], {x, y}});
}
}
}
// rep(i,0,r)
// {
// rep(j,0,c)cout<<best[i][j]<<' ';
// cout<<'\n';
// }
if (best[f.F][f.S] == 1e9)
best[f.F][f.S] = -1;
cout << best[f.F][f.S];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, s, f) for (i = s; i < f; i++)
#define print(v) \
for (auto &z : v) \
cout << z << ' '; \
cout << '\n'
#define db cout << "db: "
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define B begin()
#define E end()
#define all(v) v.B, v.E
#define sz(v) (int)((v).size())
#define vi vector<int>
#define vii vector<pair<int, int>>
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define clk1 clock_t start_time = clock()
#define clk2 cout << (clock() - start_time) / (double)CLOCKS_PER_SEC
#define clean(arr) memset(arr, 0, sizeof(arr))
#define mod 1000000007
#define mod2 998244353
#define space 100005
//<<fixed << setprecision(9)
pii mov[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
int n, i, j, t;
boost;
int r, c, k;
cin >> r >> c >> k;
pii s, f;
cin >> s.F >> s.S >> f.F >> f.S;
s.F--;
s.S--;
f.F--;
f.S--;
bool arr[r][c];
clean(arr);
rep(i, 0, r) {
string s;
cin >> s;
rep(j, 0, c) if (s[j] == '@') arr[i][j] = 1;
}
// bool vis[r][c];
// clean(vis);
int best[r][c];
rep(i, 0, r) { rep(j, 0, c) best[i][j] = 1e9; }
priority_queue<pair<int, pii>> q;
pair<int, pii> u;
int x, y;
q.push({0, s});
// vis[s.F][s.S]=1;
best[s.F][s.S] = 0;
while (!q.empty()) {
u = q.top();
// cout<<u.F<<' '<<u.S<<'\n';
q.pop();
if (-u.F > best[u.S.F][u.S.S])
continue;
for (auto &z : mov) {
j = k;
x = u.S.F;
y = u.S.S;
while (j--) {
x += z.F;
y += z.S;
if (!(x >= 0 and y >= 0 and x < r and y < c and !arr[x][y] and
best[x][y] >= 1 - u.F))
break;
if (best[x][y] == 1 - u.F)
continue;
best[x][y] = 1 - u.F;
q.push({-best[x][y], {x, y}});
}
}
}
// rep(i,0,r)
// {
// rep(j,0,c)cout<<best[i][j]<<' ';
// cout<<'\n';
// }
if (best[f.F][f.S] == 1e9)
best[f.F][f.S] = -1;
cout << best[f.F][f.S];
return 0;
}
| insert | 80 | 80 | 80 | 82 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
pair<int, int> start, goal;
cin >> start.first >> start.second;
cin >> goal.first >> goal.second;
start.first--;
start.second--;
goal.first--;
goal.second--;
vector<string> field(h);
for (int i = 0; i < h; ++i) {
cin >> field[i];
}
vector<vector<bool>> seen(h, vector<bool>(w, false));
vector<vector<int>> dist(h, vector<int>(w, -1));
dist[start.first][start.second] = 0;
seen[start.first][start.second] = true;
queue<pair<int, int>> next;
next.push(start);
while (!next.empty()) {
int x = next.front().first;
int y = next.front().second;
next.pop();
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
int nx = x + dx[i] * j;
int ny = y + dy[i] * j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (field[nx][ny] == '@')
break;
if (seen[nx][ny] && dist[nx][ny] < dist[x][y] + 1)
break;
dist[nx][ny] = dist[x][y] + 1;
next.push({nx, ny});
seen[nx][ny] = true;
}
}
}
cout << dist[goal.first][goal.second];
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
pair<int, int> start, goal;
cin >> start.first >> start.second;
cin >> goal.first >> goal.second;
start.first--;
start.second--;
goal.first--;
goal.second--;
vector<string> field(h);
for (int i = 0; i < h; ++i) {
cin >> field[i];
}
vector<vector<bool>> seen(h, vector<bool>(w, false));
vector<vector<int>> dist(h, vector<int>(w, -1));
dist[start.first][start.second] = 0;
seen[start.first][start.second] = true;
queue<pair<int, int>> next;
next.push(start);
while (!next.empty()) {
int x = next.front().first;
int y = next.front().second;
next.pop();
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
int nx = x + dx[i] * j;
int ny = y + dy[i] * j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (field[nx][ny] == '@')
break;
if (seen[nx][ny] && dist[nx][ny] < dist[x][y] + 1)
break;
dist[nx][ny] = dist[x][y] + 1;
if (!seen[nx][ny])
next.push({nx, ny});
seen[nx][ny] = true;
}
}
}
cout << dist[goal.first][goal.second];
} | replace | 43 | 44 | 43 | 45 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
pair<int, int> start, goal;
cin >> start.first >> start.second;
start.first--;
start.second--;
cin >> goal.first >> goal.second;
goal.first--;
goal.second--;
vector<string> field(h);
for (int i = 0; i < h; ++i) {
cin >> field[i];
}
vector<vector<ll>> distance(h, vector<ll>(w, -1));
vector<vector<bool>> seen(h, vector<bool>(w, false));
distance[start.first][start.second] = 0;
queue<pair<int, int>> next;
next.push(start);
seen[start.first][start.second] = true;
while (!next.empty()) {
int x = next.front().first;
int y = next.front().second;
next.pop();
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
int nx = x + j * dx[i], ny = y + j * dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (field[nx][ny] == '@')
break;
if (seen[nx][ny]) {
distance[nx][ny] = min(distance[nx][ny], distance[x][y] + 1);
continue;
} else
distance[nx][ny] = distance[x][y] + 1;
seen[nx][ny] = true;
next.push({nx, ny});
}
}
}
cout << distance[goal.first][goal.second] << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
pair<int, int> start, goal;
cin >> start.first >> start.second;
start.first--;
start.second--;
cin >> goal.first >> goal.second;
goal.first--;
goal.second--;
vector<string> field(h);
for (int i = 0; i < h; ++i) {
cin >> field[i];
}
vector<vector<ll>> distance(h, vector<ll>(w, -1));
vector<vector<bool>> seen(h, vector<bool>(w, false));
distance[start.first][start.second] = 0;
queue<pair<int, int>> next;
next.push(start);
seen[start.first][start.second] = true;
while (!next.empty()) {
int x = next.front().first;
int y = next.front().second;
next.pop();
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= k; ++j) {
int nx = x + j * dx[i], ny = y + j * dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w)
break;
if (field[nx][ny] == '@')
break;
if (seen[nx][ny]) {
if (distance[nx][ny] < distance[x][y] + 1)
break;
distance[nx][ny] = min(distance[nx][ny], distance[x][y] + 1);
continue;
} else
distance[nx][ny] = distance[x][y] + 1;
seen[nx][ny] = true;
next.push({nx, ny});
}
}
}
cout << distance[goal.first][goal.second] << endl;
} | insert | 40 | 40 | 40 | 42 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define MOD (long long int)(1e9 + 7)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
if (n <= 1e7) {
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
} else {
rep(i, r) {
ret *= n - i;
ret %= MOD;
ret *= mpow(r - i, MOD - 2);
ret %= MOD;
}
}
return ret;
}
struct hoge {
ll y, x, t;
pair<ll, ll> d;
};
int main(void) {
fast_io cout << fixed << setprecision(15);
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vector<string> S(h);
rep(y, h) { cin >> S[y]; }
// dp[y][x][j]:
// (y,x)に行く方法のうち最後の移動がUDLR[j]であるもののうち、最小手数,
// 最小歩数
vector<vector<vector<pair<ll, ll>>>> dp(h);
rep(y, h) {
dp[y] = vector<vector<pair<ll, ll>>>(w);
rep(x, w) { dp[y][x] = vector<pair<ll, ll>>(4, {LINF, LINF}); }
}
auto cmp = [](const hoge &l, const hoge &r) { return l.d < r.d; };
priority_queue<hoge, vector<hoge>, decltype(cmp)> q(cmp);
rep(t, 4) { q.push({y1, x1, t, {0, 0}}); }
while (q.size() > 0) {
hoge fuga = q.top();
q.pop();
ll y = fuga.y;
ll x = fuga.x;
if (y < 0 || y >= h || x < 0 || x >= w)
continue;
if (S[y][x] == '@')
continue;
ll t = fuga.t;
auto d = fuga.d;
if (dp[y][x][t] < d)
continue;
dp[y][x][t] = d;
rep(ti, 4) {
auto c = d;
if (ti == t) {
c.second++;
if (c.second == k + 1) {
c.first++;
c.second = 1;
}
} else {
c.first++;
c.second = 1;
}
ll ny = y, nx = x;
if (ti == 0) {
ny = y - 1;
} else if (ti == 1) {
ny = y + 1;
} else if (ti == 2) {
nx = x - 1;
} else {
nx = x + 1;
}
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (dp[ny][nx][ti] <= c)
continue;
dp[ny][nx][ti] = c;
q.push({ny, nx, ti, c});
}
}
pair<ll, ll> ans = {LINF, LINF};
rep(t, 4) {
pair<ll, ll> fuga = dp[y2][x2][t];
if (fuga.first == LINF)
continue;
if (fuga.second > 0) {
fuga.first++;
fuga.second = 0;
}
chmin(ans, fuga);
}
if (ans.first == LINF) {
cout << -1 << endl;
return 0;
}
cout << ans.first << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define MOD (long long int)(1e9 + 7)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
if (n <= 1e7) {
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
} else {
rep(i, r) {
ret *= n - i;
ret %= MOD;
ret *= mpow(r - i, MOD - 2);
ret %= MOD;
}
}
return ret;
}
struct hoge {
ll y, x, t;
pair<ll, ll> d;
};
int main(void) {
fast_io cout << fixed << setprecision(15);
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vector<string> S(h);
rep(y, h) { cin >> S[y]; }
// dp[y][x][j]:
// (y,x)に行く方法のうち最後の移動がUDLR[j]であるもののうち、最小手数,
// 最小歩数
vector<vector<vector<pair<ll, ll>>>> dp(h);
rep(y, h) {
dp[y] = vector<vector<pair<ll, ll>>>(w);
rep(x, w) { dp[y][x] = vector<pair<ll, ll>>(4, {LINF, LINF}); }
}
auto cmp = [](const hoge &l, const hoge &r) { return l.d > r.d; };
priority_queue<hoge, vector<hoge>, decltype(cmp)> q(cmp);
rep(t, 4) { q.push({y1, x1, t, {0, 0}}); }
while (q.size() > 0) {
hoge fuga = q.top();
q.pop();
ll y = fuga.y;
ll x = fuga.x;
if (y < 0 || y >= h || x < 0 || x >= w)
continue;
if (S[y][x] == '@')
continue;
ll t = fuga.t;
auto d = fuga.d;
if (dp[y][x][t] < d)
continue;
dp[y][x][t] = d;
rep(ti, 4) {
auto c = d;
if (ti == t) {
c.second++;
if (c.second == k + 1) {
c.first++;
c.second = 1;
}
} else {
c.first++;
c.second = 1;
}
ll ny = y, nx = x;
if (ti == 0) {
ny = y - 1;
} else if (ti == 1) {
ny = y + 1;
} else if (ti == 2) {
nx = x - 1;
} else {
nx = x + 1;
}
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
continue;
if (dp[ny][nx][ti] <= c)
continue;
dp[ny][nx][ti] = c;
q.push({ny, nx, ti, c});
}
}
pair<ll, ll> ans = {LINF, LINF};
rep(t, 4) {
pair<ll, ll> fuga = dp[y2][x2][t];
if (fuga.first == LINF)
continue;
if (fuga.second > 0) {
fuga.first++;
fuga.second = 0;
}
chmin(ans, fuga);
}
if (ans.first == LINF) {
cout << -1 << endl;
return 0;
}
cout << ans.first << endl;
return 0;
} | replace | 134 | 135 | 134 | 135 | TLE | |
p02644 | C++ | Runtime Error | // ダイクストラ法
#include "bits/stdc++.h"
#include <queue>
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
ll n, k;
const int N = 1e5;
vector<vector<pair<int, double>>> g;
using pairdi = pair<double, int>;
vector<double>
leng(N, 4503599627370496.0); // ある点を始点として、全体nodeへの最短距離
// 小さいほうが先頭にくるよう。ダイクストラ法などで使用を想定
// doubleは長さ、intはnodeidを意味している
auto compare = [](pairdi a, pairdi b) { return a.first > b.first; };
priority_queue<pairdi, vector<pairdi>, decltype(compare)> que{compare};
// 今見ている頂点から伸びる頂点をqueに溜める
void pushg(int nodeid) {
double len = leng[nodeid];
for (auto to : g[nodeid]) {
if (to.second == 100.0) {
double dtmp = fmod(len, double(k));
to.second = (double)k - dtmp;
}
que.emplace(len + to.second, to.first); // 長さ,nodeid
}
}
void djk(int first_node, int second_node) {
// 初期値をプライオリティーキューにいれる
que.emplace(0.0, first_node);
que.emplace(0.0, second_node);
// キューがなくなるまでやる
int nodeid;
double ddlen;
while (!que.empty()) {
nodeid = que.top().second;
ddlen = que.top().first;
if (leng[nodeid] > ddlen) {
leng[nodeid] = ddlen;
pushg(nodeid);
}
que.pop();
}
return;
}
int main() {
ll h, w;
cin >> h >> w >> k;
n = h * w;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vvi data(w, vi(h)); // N * M の2次元配列
g.resize(n * 2 + 1);
for (ll i = 0; i < h; i++) {
string s;
cin >> s;
for (ll j = 0; j < w; j++) {
data[j][i] = (s[j] == '.');
}
}
for (ll i = 0; i < w; i++) {
for (ll j = 0; j < h; j++) {
ll a = j * w + i;
ll an = j * w + i + n;
ll b = j * w + i;
ll bn = j * w + i + n;
if (i != 0) {
if (data[i - 1][j]) {
g[a].emplace_back(a - 1, 1.0);
g[a].emplace_back(a + n - 1, 100.0);
}
}
if (i != w - 1) {
if (data[i + 1][j]) {
g[a].emplace_back(a + 1, 1.0);
g[a].emplace_back(a + n + 1, 100.0);
}
}
if (j != 0) {
if (data[i][j - 1]) {
g[a + n].emplace_back(a - w, 100.0);
g[a + n].emplace_back(a + n - w, 1.0);
}
}
if (j != h - 1) {
if (data[i][j + 1]) {
g[a + n].emplace_back(a + w, 100.0);
g[a + n].emplace_back(a + n + w, 1.0);
}
}
}
}
djk(x1 + y1 * w, x1 + y1 * w + n);
ll ans = min((ll)(leng[x2 + y2 * w]), (ll)(leng[x2 + y2 * w + n]));
if (ans == 4503599627370496LL) {
ans = -1;
} else {
ans = (ans - 1) / k + 1;
}
cout << ans << endl;
return 0;
} | // ダイクストラ法
#include "bits/stdc++.h"
#include <queue>
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
ll n, k;
const int N = 3e6;
vector<vector<pair<int, double>>> g;
using pairdi = pair<double, int>;
vector<double>
leng(N, 4503599627370496.0); // ある点を始点として、全体nodeへの最短距離
// 小さいほうが先頭にくるよう。ダイクストラ法などで使用を想定
// doubleは長さ、intはnodeidを意味している
auto compare = [](pairdi a, pairdi b) { return a.first > b.first; };
priority_queue<pairdi, vector<pairdi>, decltype(compare)> que{compare};
// 今見ている頂点から伸びる頂点をqueに溜める
void pushg(int nodeid) {
double len = leng[nodeid];
for (auto to : g[nodeid]) {
if (to.second == 100.0) {
double dtmp = fmod(len, double(k));
to.second = (double)k - dtmp;
}
que.emplace(len + to.second, to.first); // 長さ,nodeid
}
}
void djk(int first_node, int second_node) {
// 初期値をプライオリティーキューにいれる
que.emplace(0.0, first_node);
que.emplace(0.0, second_node);
// キューがなくなるまでやる
int nodeid;
double ddlen;
while (!que.empty()) {
nodeid = que.top().second;
ddlen = que.top().first;
if (leng[nodeid] > ddlen) {
leng[nodeid] = ddlen;
pushg(nodeid);
}
que.pop();
}
return;
}
int main() {
ll h, w;
cin >> h >> w >> k;
n = h * w;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vvi data(w, vi(h)); // N * M の2次元配列
g.resize(n * 2 + 1);
for (ll i = 0; i < h; i++) {
string s;
cin >> s;
for (ll j = 0; j < w; j++) {
data[j][i] = (s[j] == '.');
}
}
for (ll i = 0; i < w; i++) {
for (ll j = 0; j < h; j++) {
ll a = j * w + i;
ll an = j * w + i + n;
ll b = j * w + i;
ll bn = j * w + i + n;
if (i != 0) {
if (data[i - 1][j]) {
g[a].emplace_back(a - 1, 1.0);
g[a].emplace_back(a + n - 1, 100.0);
}
}
if (i != w - 1) {
if (data[i + 1][j]) {
g[a].emplace_back(a + 1, 1.0);
g[a].emplace_back(a + n + 1, 100.0);
}
}
if (j != 0) {
if (data[i][j - 1]) {
g[a + n].emplace_back(a - w, 100.0);
g[a + n].emplace_back(a + n - w, 1.0);
}
}
if (j != h - 1) {
if (data[i][j + 1]) {
g[a + n].emplace_back(a + w, 100.0);
g[a + n].emplace_back(a + n + w, 1.0);
}
}
}
}
djk(x1 + y1 * w, x1 + y1 * w + n);
ll ans = min((ll)(leng[x2 + y2 * w]), (ll)(leng[x2 + y2 * w + n]));
if (ans == 4503599627370496LL) {
ans = -1;
} else {
ans = (ans - 1) / k + 1;
}
cout << ans << endl;
return 0;
} | replace | 13 | 14 | 13 | 14 | 0 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;
int main() {
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cin >> vec[i][j];
}
}
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
P start = {x1, y1};
queue<P> Q;
Q.push(start);
dist[x1][y1] = 0;
while (Q.size()) {
P p = Q.front();
Q.pop();
int nx = p.first;
int ny = p.second;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
break;
} else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
break;
} else {
dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
P q = {nx + dx[i] * j, ny + dy[i] * j};
Q.push(q);
}
}
}
}
if (dist[x2][y2] == INF) {
dist[x2][y2] = -1;
}
cout << dist[x2][y2] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;
int main() {
int H, W, K;
cin >> H >> W >> K;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cin >> vec[i][j];
}
}
vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};
P start = {x1, y1};
queue<P> Q;
Q.push(start);
dist[x1][y1] = 0;
while (Q.size()) {
P p = Q.front();
Q.pop();
int nx = p.first;
int ny = p.second;
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= K; j++) {
if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
break;
} else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
break;
} else if (dist[nx + dx[i] * j][ny + dy[i] * j] > dist[nx][ny] + 1) {
dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
P q = {nx + dx[i] * j, ny + dy[i] * j};
Q.push(q);
}
}
}
}
if (dist[x2][y2] == INF) {
dist[x2][y2] = -1;
}
cout << dist[x2][y2] << endl;
}
| replace | 37 | 38 | 37 | 38 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int H, W, K, x1, x2, Y1, y2;
string c[1000006];
string tmp;
vector<vector<vector<long long int>>> dis, check;
long long int INF = 9999999999999999;
set<pair<long long int, pair<pair<int, int>, int>>> S;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
priority_queue<pair<long long int, pair<pair<int, int>, int>>> PQ;
int main(void) {
cin >> H >> W >> K;
cin >> x1 >> Y1 >> x2 >> y2;
for (int i = 0; i <= W + 1; i++) {
c[0] += "@";
c[H + 1] += "@";
}
for (int i = 1; i <= H; i++) {
cin >> tmp;
c[i] = "@" + tmp + "@";
}
dis.resize(H + 2);
check.resize(H + 2);
for (int i = 0; i <= H + 1; i++) {
dis[i].resize(W + 2);
check[i].resize(W + 2);
for (int j = 0; j <= W + 1; j++) {
dis[i][j].resize(4);
check[i][j].resize(4);
}
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
for (int k = 0; k < 4; k++) {
dis[i][j][k] = INF;
if (i == x1 && j == Y1) {
dis[i][j][k] = 0;
}
if (c[i][j] != '@') {
// S.insert(make_pair(dis[i][j][k], make_pair(make_pair(i, j), k)));
PQ.push(make_pair(-dis[i][j][k], make_pair(make_pair(i, j), k)));
}
}
}
}
/* while (S.size() > 0) {
auto it = S.begin();
long long int tmp_dis = (*it).first;
int x = (*it).second.first.first;
int y = (*it).second.first.second;
int k = (*it).second.second;
if (tmp_dis != dis[x][y][k]) { continue; }
dis[x][y][k] = (*it).first;
check[x][y][k] = 1;
//cout << "x= " << x << " y= " <<y << " k= " << k << " dis=
"<<dis[x][y][k]<<endl;
S.erase(it);
for (int u = 0; u < 4; u++) {
long long int ndis;
int nx, ny;
if (u == k) {
ndis = dis[x][y][k] + 1;
nx = x+dx[k];
ny = y+dy[k];
}
else {
ndis = ((dis[x][y][k]+ K - 1) / K)*K;
nx = x;
ny = y;
}
pair<long long int, pair<pair<int, int>, int> > tmp;
tmp.first = dis[nx][ny][u];
tmp.second.first.first = nx;
tmp.second.first.second = ny;
tmp.second.second = u;
S.erase(tmp);
tmp.first = ndis; //cout << "ndis= " << ndis << endl;
if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
//cout << "insert" << endl;
dis[nx][ny][u] = ndis; S.insert(tmp);
}
}
}
*/
while (PQ.size() > 0) {
auto t = PQ.top();
long long int tmp_dis = -t.first;
int x = t.second.first.first;
int y = t.second.first.second;
int k = t.second.second;
// cout << "x= " << x << " y= " << y << " k= " << k << " dis= " << tmp_dis
// << endl;
PQ.pop();
if (tmp_dis > dis[x][y][k]) {
continue;
}
dis[x][y][k] = -t.first;
check[x][y][k] = 1;
for (int u = 0; u < 4; u++) {
long long int ndis;
int nx, ny;
if (u == k) {
ndis = dis[x][y][k] + 1;
nx = x + dx[k];
ny = y + dy[k];
} else {
ndis = ((dis[x][y][k] + K - 1) / K) * K;
nx = x;
ny = y;
}
pair<long long int, pair<pair<int, int>, int>> tmp;
tmp.first = -dis[nx][ny][u];
tmp.second.first.first = nx;
tmp.second.first.second = ny;
tmp.second.second = u;
// S.erase(tmp);
tmp.first = -ndis; // cout << "ndis= " << ndis << endl;
if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
// cout << "insert" << endl;
dis[nx][ny][u] = ndis; // S.insert(tmp);
PQ.push(tmp);
}
}
}
long long int ans = INF;
for (int k = 0; k < 4; k++) {
ans = min(ans, dis[x2][y2][k]);
}
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ((ans + K - 1) / K) << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int H, W, K, x1, x2, Y1, y2;
string c[1000006];
string tmp;
vector<vector<vector<long long int>>> dis, check;
long long int INF = 9999999999999999;
set<pair<long long int, pair<pair<int, int>, int>>> S;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
priority_queue<pair<long long int, pair<pair<int, int>, int>>> PQ;
int main(void) {
cin >> H >> W >> K;
cin >> x1 >> Y1 >> x2 >> y2;
for (int i = 0; i <= W + 1; i++) {
c[0] += "@";
c[H + 1] += "@";
}
for (int i = 1; i <= H; i++) {
cin >> tmp;
c[i] = "@" + tmp + "@";
}
dis.resize(H + 2);
check.resize(H + 2);
for (int i = 0; i <= H + 1; i++) {
dis[i].resize(W + 2);
check[i].resize(W + 2);
for (int j = 0; j <= W + 1; j++) {
dis[i][j].resize(4);
check[i][j].resize(4);
}
}
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
for (int k = 0; k < 4; k++) {
dis[i][j][k] = INF;
if (i == x1 && j == Y1) {
dis[i][j][k] = 0;
}
if (c[i][j] != '@') {
// S.insert(make_pair(dis[i][j][k], make_pair(make_pair(i, j), k)));
PQ.push(make_pair(-dis[i][j][k], make_pair(make_pair(i, j), k)));
}
}
}
}
/* while (S.size() > 0) {
auto it = S.begin();
long long int tmp_dis = (*it).first;
int x = (*it).second.first.first;
int y = (*it).second.first.second;
int k = (*it).second.second;
if (tmp_dis != dis[x][y][k]) { continue; }
dis[x][y][k] = (*it).first;
check[x][y][k] = 1;
//cout << "x= " << x << " y= " <<y << " k= " << k << " dis=
"<<dis[x][y][k]<<endl;
S.erase(it);
for (int u = 0; u < 4; u++) {
long long int ndis;
int nx, ny;
if (u == k) {
ndis = dis[x][y][k] + 1;
nx = x+dx[k];
ny = y+dy[k];
}
else {
ndis = ((dis[x][y][k]+ K - 1) / K)*K;
nx = x;
ny = y;
}
pair<long long int, pair<pair<int, int>, int> > tmp;
tmp.first = dis[nx][ny][u];
tmp.second.first.first = nx;
tmp.second.first.second = ny;
tmp.second.second = u;
S.erase(tmp);
tmp.first = ndis; //cout << "ndis= " << ndis << endl;
if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
//cout << "insert" << endl;
dis[nx][ny][u] = ndis; S.insert(tmp);
}
}
}
*/
while (PQ.size() > 0) {
auto t = PQ.top();
long long int tmp_dis = -t.first;
int x = t.second.first.first;
int y = t.second.first.second;
int k = t.second.second;
// cout << "x= " << x << " y= " << y << " k= " << k << " dis= " << tmp_dis
// << endl;
PQ.pop();
if (tmp_dis > dis[x][y][k]) {
continue;
}
dis[x][y][k] = -t.first;
check[x][y][k] = 1;
for (int u = 0; u < 4; u++) {
long long int ndis;
int nx, ny;
if (u == k) {
ndis = dis[x][y][k] + 1;
nx = x + dx[k];
ny = y + dy[k];
} else {
ndis = ((dis[x][y][k] + K - 1) / K) * K;
nx = x;
ny = y;
}
pair<long long int, pair<pair<int, int>, int>> tmp;
tmp.first = -dis[nx][ny][u];
tmp.second.first.first = nx;
tmp.second.first.second = ny;
tmp.second.second = u;
// S.erase(tmp);
tmp.first = -ndis; // cout << "ndis= " << ndis << endl;
if ((c[nx][ny] == '.') && (check[nx][ny][u] == 0)) {
// cout << "insert" << endl;
if (dis[nx][ny][u] > ndis) {
dis[nx][ny][u] = ndis; // S.insert(tmp);
PQ.push(tmp);
}
}
}
}
long long int ans = INF;
for (int k = 0; k < 4; k++) {
ans = min(ans, dis[x2][y2][k]);
}
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ((ans + K - 1) / K) << endl;
}
return 0;
} | replace | 153 | 155 | 153 | 157 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define M_PI 3.14159265358979323846 // pi
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;
#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)
#include <bits/stdc++.h>
using namespace std;
static const ll INF = 1e15;
static const ll mod = 1e9 + 7;
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vector<vector<bool>> grid(h, vector<bool>(w, true));
rep(y, h) {
string s;
cin >> s;
rep(x, w) {
if (s[x] == '@') {
grid[y][x] = false;
}
}
}
vector<vector<vector<ll>>> costs(h,
vector<vector<ll>>(w, vector<ll>(4, INF)));
priority_queue<t4, vector<t4>, less<t4>> qs;
for (int i = 0; i < 4; i++) {
qs.emplace(0, x1, y1, i);
costs[y1][x1][i] = 0;
}
constexpr P shifts[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!qs.empty()) {
ll x, y, d, c;
tie(c, x, y, d) = qs.top();
qs.pop();
for (int i = 0; i < 4; i++) {
if (d == i)
continue;
auto ncost = c % k ? (c / k + 1) * k : c;
if (costs[y][x][i] <= ncost)
continue;
costs[y][x][i] = ncost;
qs.emplace(ncost, x, y, i);
}
{
auto nx = x + shifts[d].first;
auto ny = y + shifts[d].second;
if (nx < 0 || nx >= w)
continue;
if (ny < 0 || ny >= h)
continue;
if (!grid[ny][nx])
continue;
ll ncost = c + 1;
if (costs[ny][nx][d] <= ncost)
continue;
costs[ny][nx][d] = ncost;
qs.emplace(ncost, nx, ny, d);
}
}
ll low = INF;
for (int i = 0; i < 4; i++) {
auto c = costs[y2][x2][i];
if (c != INF) {
auto ncost = c % k ? (c / k + 1) : c / k;
low = min(low, ncost);
}
}
if (low == INF) {
cout << -1 << endl;
} else {
cout << low << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define M_PI 3.14159265358979323846 // pi
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;
#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)
#include <bits/stdc++.h>
using namespace std;
static const ll INF = 1e15;
static const ll mod = 1e9 + 7;
int main() {
ll h, w, k;
cin >> h >> w >> k;
ll x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
x1--;
y1--;
x2--;
y2--;
vector<vector<bool>> grid(h, vector<bool>(w, true));
rep(y, h) {
string s;
cin >> s;
rep(x, w) {
if (s[x] == '@') {
grid[y][x] = false;
}
}
}
vector<vector<vector<ll>>> costs(h,
vector<vector<ll>>(w, vector<ll>(4, INF)));
priority_queue<t4, vector<t4>, greater<t4>> qs;
for (int i = 0; i < 4; i++) {
qs.emplace(0, x1, y1, i);
costs[y1][x1][i] = 0;
}
constexpr P shifts[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!qs.empty()) {
ll x, y, d, c;
tie(c, x, y, d) = qs.top();
qs.pop();
for (int i = 0; i < 4; i++) {
if (d == i)
continue;
auto ncost = c % k ? (c / k + 1) * k : c;
if (costs[y][x][i] <= ncost)
continue;
costs[y][x][i] = ncost;
qs.emplace(ncost, x, y, i);
}
{
auto nx = x + shifts[d].first;
auto ny = y + shifts[d].second;
if (nx < 0 || nx >= w)
continue;
if (ny < 0 || ny >= h)
continue;
if (!grid[ny][nx])
continue;
ll ncost = c + 1;
if (costs[ny][nx][d] <= ncost)
continue;
costs[ny][nx][d] = ncost;
qs.emplace(ncost, nx, ny, d);
}
}
ll low = INF;
for (int i = 0; i < 4; i++) {
auto c = costs[y2][x2][i];
if (c != INF) {
auto ncost = c % k ? (c / k + 1) : c / k;
low = min(low, ncost);
}
}
if (low == INF) {
cout << -1 << endl;
} else {
cout << low << endl;
}
return 0;
}
| replace | 44 | 45 | 44 | 45 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// long long
using ll = long long;
// pair<int, int>
using PII = pair<int, int>;
// 最大値、mod
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
// 出力系
#define print(x) cout << x << endl
#define prints(x) cout << fixed << setprecision(20) << x << endl
#define printc(x) cout << setw(2) << setfill('0') << x << endl;
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
// begin() end()
#define all(x) (x).begin(), (x).end()
// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }
// a = max(a, b), a = min(a, b)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 階乗(MODをとる)
ll pow_mod(ll num, ll pow, ll mod) {
ll prod = 1;
num %= mod;
while (pow > 0) {
if (pow & 1)
prod = prod * num % mod;
num = num * num % mod;
pow >>= 1;
}
return prod;
}
// 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度)
// COMinit()
// COM(x, y)
// とコンビで使う
// テーブルを作る前処理
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// UnionFind
struct UnionFind {
vector<int> par;
vector<int> rank;
vector<ll> Size;
UnionFind(int n = 1) { init(n); }
void init(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
Size.resize(n + 1);
for (int i = 0; i <= n; ++i)
par[i] = i, rank[i] = 0, Size[i] = 1;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
return par[x] = r;
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y);
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
Size[x] += Size[y];
return true;
}
ll size(int x) { return Size[root(x)]; }
};
// modint構造体
struct Mint {
int val;
Mint inv() const {
int tmp, a = val, b = mod, x = 1, y = 0;
while (b)
tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
return Mint(x);
}
public:
Mint() : val(0) {}
Mint(ll x) {
if ((val = x % mod) < 0)
val += mod;
}
Mint pow(ll t) {
Mint res = 1, b = *this;
while (t) {
if (t & 1)
res *= b;
b *= b;
t >>= 1;
}
return res;
}
Mint &operator+=(const Mint &x) {
if ((val += x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator-=(const Mint &x) {
if ((val += mod - x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator*=(const Mint &x) {
val = (ll)val * x.val % mod;
return *this;
}
Mint &operator/=(const Mint &x) { return *this *= x.inv(); }
bool operator==(const Mint &x) const { return val == x.val; }
bool operator!=(const Mint &x) const { return val != x.val; }
bool operator<(const Mint &x) const { return val < x.val; }
bool operator<=(const Mint &x) const { return val <= x.val; }
bool operator>(const Mint &x) const { return val > x.val; }
bool operator>=(const Mint &x) const { return val >= x.val; }
Mint operator+(const Mint &x) const { return Mint(*this) += x; }
Mint operator-(const Mint &x) const { return Mint(*this) -= x; }
Mint operator*(const Mint &x) const { return Mint(*this) *= x; }
Mint operator/(const Mint &x) const { return Mint(*this) /= x; }
};
struct factorial {
vector<Mint> Fact, Finv;
public:
// factorial fact(10000010);
// fact.nCr(a, b)
// 「fact」の部分は自由に名前変更可能
factorial(int maxx) {
Fact.resize(maxx + 1), Finv.resize(maxx + 1);
Fact[0] = Mint(1);
rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1);
Finv[maxx] = Mint(1) / Fact[maxx];
rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i;
}
Mint fact(int n, bool inv = 0) {
if (inv)
return Finv[n];
else
return Fact[n];
}
Mint nPr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[n - r];
}
Mint nCr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[r] * Finv[n - r];
}
};
// 1 * 2 * 3 .... * n (mod)
ll modfact(ll n) {
if (n <= 1)
return 1;
return (n * modfact(n - 1)) % MOD;
}
// kが角度だった場合:cos(k * (PI / 180));
const double PI = acos(-1);
// 多次元vector生成 例: auto dp = make_vec<long long>(N+1, 5, 5, 5);
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// 素因数分解
vector<pair<ll, int>> factorize(ll n) {
vector<pair<ll, int>> res;
for (ll i = 2; i * i <= n; ++i) {
if (n % i)
continue;
res.emplace_back(i, 0);
while (n % i == 0) {
n /= i;
res.back().second++;
}
}
if (n != 1)
res.emplace_back(n, 1);
return res;
}
// 素数判定(マイナスは素数でないと判定するタイプ)
bool primejudge(ll a) {
if (a <= 1)
return false;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int main() {
int h, w, k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
string c[100010];
REP(i, h) cin >> c[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
queue<PII> que;
que.push({sx, sy});
dist[sx][sy] = 0;
while (!que.empty()) {
PII p = que.front();
que.pop();
REP(j, 4) {
for (int i = 1; i <= k; i++) {
int ny = p.first + (dy[j] * i), nx = p.second + (dx[j] * i);
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
break;
if (c[ny][nx] == '@')
break;
// if(dist[ny][nx] != -1) continue;
if (dist[ny][nx] > dist[p.first][p.second] + 1) {
dist[ny][nx] = dist[p.first][p.second] + 1;
que.push({ny, nx});
}
}
}
}
dist[gx][gy] != INF ? print(dist[gx][gy]) : print(-1);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// long long
using ll = long long;
// pair<int, int>
using PII = pair<int, int>;
// 最大値、mod
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
// 出力系
#define print(x) cout << x << endl
#define prints(x) cout << fixed << setprecision(20) << x << endl
#define printc(x) cout << setw(2) << setfill('0') << x << endl;
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
// begin() end()
#define all(x) (x).begin(), (x).end()
// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }
// a = max(a, b), a = min(a, b)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// 階乗(MODをとる)
ll pow_mod(ll num, ll pow, ll mod) {
ll prod = 1;
num %= mod;
while (pow > 0) {
if (pow & 1)
prod = prod * num % mod;
num = num * num % mod;
pow >>= 1;
}
return prod;
}
// 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度)
// COMinit()
// COM(x, y)
// とコンビで使う
// テーブルを作る前処理
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// UnionFind
struct UnionFind {
vector<int> par;
vector<int> rank;
vector<ll> Size;
UnionFind(int n = 1) { init(n); }
void init(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
Size.resize(n + 1);
for (int i = 0; i <= n; ++i)
par[i] = i, rank[i] = 0, Size[i] = 1;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
return par[x] = r;
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y);
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
Size[x] += Size[y];
return true;
}
ll size(int x) { return Size[root(x)]; }
};
// modint構造体
struct Mint {
int val;
Mint inv() const {
int tmp, a = val, b = mod, x = 1, y = 0;
while (b)
tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
return Mint(x);
}
public:
Mint() : val(0) {}
Mint(ll x) {
if ((val = x % mod) < 0)
val += mod;
}
Mint pow(ll t) {
Mint res = 1, b = *this;
while (t) {
if (t & 1)
res *= b;
b *= b;
t >>= 1;
}
return res;
}
Mint &operator+=(const Mint &x) {
if ((val += x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator-=(const Mint &x) {
if ((val += mod - x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator*=(const Mint &x) {
val = (ll)val * x.val % mod;
return *this;
}
Mint &operator/=(const Mint &x) { return *this *= x.inv(); }
bool operator==(const Mint &x) const { return val == x.val; }
bool operator!=(const Mint &x) const { return val != x.val; }
bool operator<(const Mint &x) const { return val < x.val; }
bool operator<=(const Mint &x) const { return val <= x.val; }
bool operator>(const Mint &x) const { return val > x.val; }
bool operator>=(const Mint &x) const { return val >= x.val; }
Mint operator+(const Mint &x) const { return Mint(*this) += x; }
Mint operator-(const Mint &x) const { return Mint(*this) -= x; }
Mint operator*(const Mint &x) const { return Mint(*this) *= x; }
Mint operator/(const Mint &x) const { return Mint(*this) /= x; }
};
struct factorial {
vector<Mint> Fact, Finv;
public:
// factorial fact(10000010);
// fact.nCr(a, b)
// 「fact」の部分は自由に名前変更可能
factorial(int maxx) {
Fact.resize(maxx + 1), Finv.resize(maxx + 1);
Fact[0] = Mint(1);
rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1);
Finv[maxx] = Mint(1) / Fact[maxx];
rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i;
}
Mint fact(int n, bool inv = 0) {
if (inv)
return Finv[n];
else
return Fact[n];
}
Mint nPr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[n - r];
}
Mint nCr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[r] * Finv[n - r];
}
};
// 1 * 2 * 3 .... * n (mod)
ll modfact(ll n) {
if (n <= 1)
return 1;
return (n * modfact(n - 1)) % MOD;
}
// kが角度だった場合:cos(k * (PI / 180));
const double PI = acos(-1);
// 多次元vector生成 例: auto dp = make_vec<long long>(N+1, 5, 5, 5);
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// 素因数分解
vector<pair<ll, int>> factorize(ll n) {
vector<pair<ll, int>> res;
for (ll i = 2; i * i <= n; ++i) {
if (n % i)
continue;
res.emplace_back(i, 0);
while (n % i == 0) {
n /= i;
res.back().second++;
}
}
if (n != 1)
res.emplace_back(n, 1);
return res;
}
// 素数判定(マイナスは素数でないと判定するタイプ)
bool primejudge(ll a) {
if (a <= 1)
return false;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int main() {
int h, w, k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
string c[100010];
REP(i, h) cin >> c[i];
vector<vector<int>> dist(h, vector<int>(w, INF));
queue<PII> que;
que.push({sx, sy});
dist[sx][sy] = 0;
while (!que.empty()) {
PII p = que.front();
que.pop();
REP(j, 4) {
for (int i = 1; i <= k; i++) {
int ny = p.first + (dy[j] * i), nx = p.second + (dx[j] * i);
if (ny < 0 || ny >= h || nx < 0 || nx >= w)
break;
if (c[ny][nx] == '@')
break;
if (dist[ny][nx] <= dist[p.first][p.second])
break;
if (dist[ny][nx] > dist[p.first][p.second] + 1) {
dist[ny][nx] = dist[p.first][p.second] + 1;
que.push({ny, nx});
}
}
}
}
dist[gx][gy] != INF ? print(dist[gx][gy]) : print(-1);
return 0;
} | replace | 295 | 296 | 295 | 297 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
int x1, y1, x2, y2;
cin >> H >> W >> K;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> S(H);
for (int i = 0; i < H; i++)
cin >> S[i];
vector<vector<int>> D(H, vector<int>(W, INF));
queue<pair<int, int>> P;
D[x1][y1] = 0;
P.push({x1, y1});
while (!P.empty()) {
int x = P.front().first, y = P.front().second;
P.pop();
int nd = D[x][y] + 1;
for (int r = 0; r < 4; r++) {
int tx = x, ty = y;
for (int i = 0; i < K; i++) {
tx += dx[r], ty += dy[r];
if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@')
break;
if (D[tx][ty] > nd) {
D[tx][ty] = nd;
P.push(make_pair(tx, ty));
}
}
}
}
int ans = D[x2][y2];
cout << (ans < INF ? ans : -1) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
int x1, y1, x2, y2;
cin >> H >> W >> K;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
vector<string> S(H);
for (int i = 0; i < H; i++)
cin >> S[i];
vector<vector<int>> D(H, vector<int>(W, INF));
queue<pair<int, int>> P;
D[x1][y1] = 0;
P.push({x1, y1});
while (!P.empty()) {
int x = P.front().first, y = P.front().second;
P.pop();
int nd = D[x][y] + 1;
for (int r = 0; r < 4; r++) {
int tx = x, ty = y;
for (int i = 0; i < K; i++) {
tx += dx[r], ty += dy[r];
if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@' ||
D[tx][ty] < nd)
break;
if (D[tx][ty] > nd) {
D[tx][ty] = nd;
P.push(make_pair(tx, ty));
}
}
}
}
int ans = D[x2][y2];
cout << (ans < INF ? ans : -1) << endl;
}
| replace | 28 | 29 | 28 | 30 | TLE | |
p02644 | C++ | Time Limit Exceeded | #pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define C complex<ld>
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i, j, k) mkp(i, mkp(j, k))
#define rng(i, s, n) for (int i = (s); i < (n); i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define vi vec<int>
#define pb emplace_back
#define siz(a) (int)(a).size()
#define crdcomp(b) \
sort(all((b))); \
(b).erase(unique(all((b))), (b).end())
#define getidx(b, i) lower_bound(all(b), (i)) - b.begin()
#define ssp(i, n) (i == (int)(n)-1 ? "\n" : " ")
#define ctoi(c) (int)(c - '0')
#define itoc(c) (char)(c + '0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) \
int quetimes_ = (n); \
rep(qq123_, quetimes_)
#define gcj printf("Case #%lld: ", qq123_ + 1)
#define readv(a, n) \
a.resize(n, 0); \
rep(i, (n)) a[i] = read()
// #define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
ll read() {
ll u, k = scanf("%lld", &u);
return u;
}
string reads() {
string s;
cin >> s;
return s;
}
H readh(bool g = 0) {
H u;
int k = scanf("%lld %lld", &u.fs, &u.sc);
if (g)
u.fs--, u.sc--;
return u;
}
bool ina(H t, int h, int w) {
return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w;
}
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll popcount(ll x) {
int sum = 0;
for (int i = 0; i < 60; i++)
if ((1ll << i) & x)
sum++;
return sum;
}
class mint {
public:
ll v;
mint(ll v = 0) { s(v % mod); }
constexpr static int mod = 1e9 + 7;
constexpr static int fn_ = 500005;
static mint fact[fn_], comp[fn_];
mint pow(mint x) const {
mint b(v), c(1);
while (x.v) {
if (x.v & 1)
c *= b;
b *= b;
x.v >>= 1;
}
return c;
}
inline mint &s(int vv) {
v = vv < mod ? vv : vv - mod;
return *this;
}
inline mint inv() const { return pow(mod - 2); }
inline mint operator-() const { return mint() - *this; }
inline mint &operator+=(const mint b) { return s(v + b.v); }
inline mint &operator-=(const mint b) { return s(v + mod - b.v); }
inline mint &operator*=(const mint b) {
v = v * b.v % mod;
return *this;
}
inline mint &operator/=(const mint b) { return (*this) *= b.inv(); }
inline mint operator+(const mint b) const { return mint(v) += b; }
inline mint operator-(const mint b) const { return mint(v) -= b; }
inline mint operator*(const mint b) const { return mint(v) *= b; }
inline mint operator/(const mint b) const { return mint(v) /= b; }
friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; }
friend istream &operator>>(istream &is, mint &m) {
int x;
is >> x;
m = mint(x);
return is;
}
bool operator<(const mint &r) const { return v < r.v; }
bool operator>(const mint &r) const { return v > r.v; }
bool operator<=(const mint &r) const { return v <= r.v; }
bool operator>=(const mint &r) const { return v >= r.v; }
bool operator==(const mint &r) const { return v == r.v; }
bool operator!=(const mint &r) const { return v != r.v; }
explicit operator bool() const { return v; }
explicit operator int() const { return v; }
mint comb(mint k) {
if (k > *this)
return mint();
if (!fact[0])
combinit();
if (v >= fn_) {
if (k > *this - k)
k = *this - k;
mint tmp(1);
for (int i = v; i >= v - k.v + 1; i--)
tmp *= mint(i);
return tmp * comp[k.v];
} else
return fact[v] * comp[k.v] * comp[v - k.v];
} // nCk
static void combinit() {
fact[0] = 1;
for (int i = 1; i < fn_; i++)
fact[i] = fact[i - 1] * mint(i);
comp[fn_ - 1] = fact[fn_ - 1].inv();
for (int i = fn_ - 2; i >= 0; i--)
comp[i] = comp[i + 1] * mint(i + 1);
}
};
mint mint::fact[fn_], mint::comp[fn_];
//--------------------------------------------------------------
//---------------------------------------------------------------------
int h, w, k;
int x1, y3, x2, y2;
signed main() {
cin >> h >> w >> k >> x1 >> y3 >> x2 >> y2;
vec<string> s(h);
vec<vec<int>> a(h, vec<int>(w, inf));
rep(i, h) cin >> s[i];
x1--;
y3--;
x2--;
y2--;
a[x1][y3] = 0;
struct st {
int x, y, a;
bool operator<(st b) const { return a < b.a; }
bool operator>(st b) const { return a > b.a; }
};
priority_queue<st, vec<st>, greater<st>> p;
p.push(st{x1, y3, 0});
while (!p.empty()) {
st t = p.top();
p.pop();
if (t.a != a[t.x][t.y])
continue;
rep(z, 4) {
bool flag = 0;
int pre = -inf;
rng(r, 1, k + 1) {
H g = H{t.x + dx[z] * r, t.y + dy[z] * r};
if (!ina(g, h, w))
break;
if (s[g.fs][g.sc] == '@' || a[g.fs][g.sc] < a[t.x][t.y])
break;
if (a[g.fs][g.sc] <= pre + 1) {
break;
}
chmin(pre, a[g.fs][g.sc]);
if (a[g.fs][g.sc] > t.a + 1) {
a[g.fs][g.sc] = t.a + 1;
p.push(st{g.fs, g.sc, t.a + 1});
}
}
}
}
if (a[x2][y2] == inf)
a[x2][y2] = -1;
cout << a[x2][y2] << endl;
}
| #pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(), (a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define C complex<ld>
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i, j, k) mkp(i, mkp(j, k))
#define rng(i, s, n) for (int i = (s); i < (n); i++)
#define rep(i, n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define vi vec<int>
#define pb emplace_back
#define siz(a) (int)(a).size()
#define crdcomp(b) \
sort(all((b))); \
(b).erase(unique(all((b))), (b).end())
#define getidx(b, i) lower_bound(all(b), (i)) - b.begin()
#define ssp(i, n) (i == (int)(n)-1 ? "\n" : " ")
#define ctoi(c) (int)(c - '0')
#define itoc(c) (char)(c + '0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) \
int quetimes_ = (n); \
rep(qq123_, quetimes_)
#define gcj printf("Case #%lld: ", qq123_ + 1)
#define readv(a, n) \
a.resize(n, 0); \
rep(i, (n)) a[i] = read()
// #define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
ll read() {
ll u, k = scanf("%lld", &u);
return u;
}
string reads() {
string s;
cin >> s;
return s;
}
H readh(bool g = 0) {
H u;
int k = scanf("%lld %lld", &u.fs, &u.sc);
if (g)
u.fs--, u.sc--;
return u;
}
bool ina(H t, int h, int w) {
return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w;
}
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll popcount(ll x) {
int sum = 0;
for (int i = 0; i < 60; i++)
if ((1ll << i) & x)
sum++;
return sum;
}
class mint {
public:
ll v;
mint(ll v = 0) { s(v % mod); }
constexpr static int mod = 1e9 + 7;
constexpr static int fn_ = 500005;
static mint fact[fn_], comp[fn_];
mint pow(mint x) const {
mint b(v), c(1);
while (x.v) {
if (x.v & 1)
c *= b;
b *= b;
x.v >>= 1;
}
return c;
}
inline mint &s(int vv) {
v = vv < mod ? vv : vv - mod;
return *this;
}
inline mint inv() const { return pow(mod - 2); }
inline mint operator-() const { return mint() - *this; }
inline mint &operator+=(const mint b) { return s(v + b.v); }
inline mint &operator-=(const mint b) { return s(v + mod - b.v); }
inline mint &operator*=(const mint b) {
v = v * b.v % mod;
return *this;
}
inline mint &operator/=(const mint b) { return (*this) *= b.inv(); }
inline mint operator+(const mint b) const { return mint(v) += b; }
inline mint operator-(const mint b) const { return mint(v) -= b; }
inline mint operator*(const mint b) const { return mint(v) *= b; }
inline mint operator/(const mint b) const { return mint(v) /= b; }
friend ostream &operator<<(ostream &os, const mint &m) { return os << m.v; }
friend istream &operator>>(istream &is, mint &m) {
int x;
is >> x;
m = mint(x);
return is;
}
bool operator<(const mint &r) const { return v < r.v; }
bool operator>(const mint &r) const { return v > r.v; }
bool operator<=(const mint &r) const { return v <= r.v; }
bool operator>=(const mint &r) const { return v >= r.v; }
bool operator==(const mint &r) const { return v == r.v; }
bool operator!=(const mint &r) const { return v != r.v; }
explicit operator bool() const { return v; }
explicit operator int() const { return v; }
mint comb(mint k) {
if (k > *this)
return mint();
if (!fact[0])
combinit();
if (v >= fn_) {
if (k > *this - k)
k = *this - k;
mint tmp(1);
for (int i = v; i >= v - k.v + 1; i--)
tmp *= mint(i);
return tmp * comp[k.v];
} else
return fact[v] * comp[k.v] * comp[v - k.v];
} // nCk
static void combinit() {
fact[0] = 1;
for (int i = 1; i < fn_; i++)
fact[i] = fact[i - 1] * mint(i);
comp[fn_ - 1] = fact[fn_ - 1].inv();
for (int i = fn_ - 2; i >= 0; i--)
comp[i] = comp[i + 1] * mint(i + 1);
}
};
mint mint::fact[fn_], mint::comp[fn_];
//--------------------------------------------------------------
//---------------------------------------------------------------------
int h, w, k;
int x1, y3, x2, y2;
signed main() {
cin >> h >> w >> k >> x1 >> y3 >> x2 >> y2;
vec<string> s(h);
vec<vec<int>> a(h, vec<int>(w, inf));
rep(i, h) cin >> s[i];
x1--;
y3--;
x2--;
y2--;
a[x1][y3] = 0;
struct st {
int x, y, a;
bool operator<(st b) const { return a < b.a; }
bool operator>(st b) const { return a > b.a; }
};
priority_queue<st, vec<st>, greater<st>> p;
p.push(st{x1, y3, 0});
while (!p.empty()) {
st t = p.top();
p.pop();
if (t.a != a[t.x][t.y])
continue;
rep(z, 4) {
bool flag = 0;
int pre = -inf;
rng(r, 1, k + 1) {
H g = H{t.x + dx[z] * r, t.y + dy[z] * r};
if (!ina(g, h, w))
break;
if (s[g.fs][g.sc] == '@' || a[g.fs][g.sc] <= a[t.x][t.y])
break;
if (a[g.fs][g.sc] <= pre + 1) {
break;
}
chmin(pre, a[g.fs][g.sc]);
if (a[g.fs][g.sc] > t.a + 1) {
a[g.fs][g.sc] = t.a + 1;
p.push(st{g.fs, g.sc, t.a + 1});
}
}
}
}
if (a[x2][y2] == inf)
a[x2][y2] = -1;
cout << a[x2][y2] << endl;
}
| replace | 227 | 228 | 227 | 228 | TLE | |
p02644 | C++ | Time Limit Exceeded | /**
* author: K.R.Surrya
* created: 15.06.2020 17:06:33
**/
//~ ~~ ~~~~ ~~~~~~~~
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define vi vector<int>
#define vli vector<lint>
#define pb push_back
#define ALL(x) begin(x), end(x)
#define inf (1LL < 63) - 1
#define MOD 1000000007
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define f_ip \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ZERO(a) fill(ALL(a), 0)
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> bool chmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool chmin(T &m, const T q) {
if (q < m) {
m = q;
return true;
} else
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/* ~Debugging~ */
/*~~~~~~~~~~~~~~~~~*/
#define LOCAL
#define sim template <class c
#define ris return *this
#define dor > deb &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, deb &>::type operator<<(c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb {
#ifdef LOCAL
~deb() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define what(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL
/* ~magic~ */
/*~~~~~~~~~~~~~~~*/
#define magic(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) { return; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...); // args is going to end.
}
/* ~driver~ */
/*~~~~~~~~~~*/
vector<int> dx = {0, 1, 0, -1}; // clockwise from east direction;
vector<int> dy = {1, 0, -1, 0};
struct mPair {
int x;
int y;
string path;
mPair() {}
mPair(int x, int y, string path) {
this->x = x;
this->y = y;
this->path = path;
}
};
bool isv(int x, int y, int n, int m) {
if (x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1)
return true;
else
return false;
}
void solve() {
int H, W, cap;
cin >> H >> W >> cap;
int sx, sy;
cin >> sx >> sy;
--sx;
--sy;
int destx, desty;
cin >> destx >> desty;
--destx;
--desty;
vector<vector<int>> mat(H, vector<int>(W));
REP(i, H) {
REP(j, W) {
char ch;
cin >> ch; // only two types of characters present in the matrix.
if (ch == '@') { // acts as a blocker;
mat[i][j] = 0;
} else {
mat[i][j] = 1;
}
}
}
vector<vector<lint>> dist(H, vector<lint>(W, 1e18));
queue<mPair> q;
q.push({sx, sy, ""});
bool ok = 0;
string check;
dist[sx][sy] = 0;
while (q.size()) {
mPair rem = q.front();
q.pop();
if (rem.x == destx && rem.y == desty) {
cout << dist[rem.x][rem.y] << '\n';
exit(0);
check = rem.path;
ok = 1;
break;
}
REP(k, 4) {
FOR(leap, 1, cap + 1) {
int nx = rem.x + dx[k] * leap;
int ny = rem.y + dy[k] * leap;
if (!isv(nx, ny, H, W))
continue;
if (mat[nx][ny] == 0)
break;
/* important */
if (dist[nx][ny] > dist[rem.x][rem.y] + 1) {
string curr = rem.path + to_string(k);
dist[nx][ny] = dist[rem.x][rem.y] + 1;
q.push({nx, ny, curr});
}
}
}
}
cout << -1 << '\n';
}
int main() {
#if 0
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T = 1;
// cin>>T;
for (int tt = 1; tt <= T; ++tt) {
solve();
}
return 0;
}
/* ~Modulus functions~ */
/*~~~~~~~~~~~~~~~~~~~~~~~*/
lint pow_mod(lint base, lint expo) {
lint ret = 1;
for (; expo;) {
if (expo & 1)
ret = (ret * base) % MOD;
expo = (expo >> 1);
base = (base * base) % MOD;
}
return ret;
}
| /**
* author: K.R.Surrya
* created: 15.06.2020 17:06:33
**/
//~ ~~ ~~~~ ~~~~~~~~
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define vi vector<int>
#define vli vector<lint>
#define pb push_back
#define ALL(x) begin(x), end(x)
#define inf (1LL < 63) - 1
#define MOD 1000000007
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define f_ip \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ZERO(a) fill(ALL(a), 0)
struct fast_ios {
fast_ios() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec)
is >> v;
return is;
}
template <typename T> bool chmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T> bool chmin(T &m, const T q) {
if (q < m) {
m = q;
return true;
} else
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/* ~Debugging~ */
/*~~~~~~~~~~~~~~~~~*/
#define LOCAL
#define sim template <class c
#define ris return *this
#define dor > deb &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, deb &>::type operator<<(c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb {
#ifdef LOCAL
~deb() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define what(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL
/* ~magic~ */
/*~~~~~~~~~~~~~~~*/
#define magic(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
void err(istream_iterator<string> it) { return; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...); // args is going to end.
}
/* ~driver~ */
/*~~~~~~~~~~*/
vector<int> dx = {0, 1, 0, -1}; // clockwise from east direction;
vector<int> dy = {1, 0, -1, 0};
struct mPair {
int x;
int y;
string path;
mPair() {}
mPair(int x, int y, string path) {
this->x = x;
this->y = y;
this->path = path;
}
};
bool isv(int x, int y, int n, int m) {
if (x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1)
return true;
else
return false;
}
void solve() {
int H, W, cap;
cin >> H >> W >> cap;
int sx, sy;
cin >> sx >> sy;
--sx;
--sy;
int destx, desty;
cin >> destx >> desty;
--destx;
--desty;
vector<vector<int>> mat(H, vector<int>(W));
REP(i, H) {
REP(j, W) {
char ch;
cin >> ch; // only two types of characters present in the matrix.
if (ch == '@') { // acts as a blocker;
mat[i][j] = 0;
} else {
mat[i][j] = 1;
}
}
}
vector<vector<lint>> dist(H, vector<lint>(W, 1e18));
queue<mPair> q;
q.push({sx, sy, ""});
bool ok = 0;
string check;
dist[sx][sy] = 0;
while (q.size()) {
mPair rem = q.front();
q.pop();
if (rem.x == destx && rem.y == desty) {
cout << dist[rem.x][rem.y] << '\n';
exit(0);
check = rem.path;
ok = 1;
break;
}
REP(k, 4) {
FOR(leap, 1, cap + 1) {
int nx = rem.x + dx[k] * leap;
int ny = rem.y + dy[k] * leap;
if (!isv(nx, ny, H, W))
continue; // validity
if (mat[nx][ny] == 0 || dist[nx][ny] <= dist[rem.x][rem.y])
break; // road_block //stop redundant work
/* important */
if (dist[nx][ny] > dist[rem.x][rem.y] + 1) {
string curr = rem.path + to_string(k);
dist[nx][ny] = dist[rem.x][rem.y] + 1;
q.push({nx, ny, curr});
}
}
}
}
cout << -1 << '\n';
}
int main() {
#if 0
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T = 1;
// cin>>T;
for (int tt = 1; tt <= T; ++tt) {
solve();
}
return 0;
}
/* ~Modulus functions~ */
/*~~~~~~~~~~~~~~~~~~~~~~~*/
lint pow_mod(lint base, lint expo) {
lint ret = 1;
for (; expo;) {
if (expo & 1)
ret = (ret * base) % MOD;
expo = (expo >> 1);
base = (base * base) % MOD;
}
return ret;
}
| replace | 181 | 184 | 181 | 184 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif
#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) \
for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }
vi dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int h, w;
bool isin(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }
signed main() {
int k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vs c(h);
rep(i, h) cin >> c[i];
vvi d(h, vi(w, inf));
d[sx][sy] = 0;
queue<pii> q;
q.push(pii(sx, sy));
while (not q.empty()) {
auto now = q.front();
q.pop();
rep(i, 4) {
FOR(j, 1, k + 1) {
int nx = now.first + j * dx[i], ny = now.second + j * dy[i];
if (not isin(nx, ny) or c[nx][ny] == '@')
break;
if (d[nx][ny] == inf) {
d[nx][ny] = d[now.first][now.second] + 1;
q.push(pii(nx, ny));
}
}
}
}
if (d[gx][gy] == inf)
cout << -1 << endl;
else
cout << d[gx][gy] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif
#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) \
for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }
vi dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int h, w;
bool isin(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }
signed main() {
int k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vs c(h);
rep(i, h) cin >> c[i];
vvi d(h, vi(w, inf));
d[sx][sy] = 0;
queue<pii> q;
q.push(pii(sx, sy));
while (not q.empty()) {
auto now = q.front();
q.pop();
rep(i, 4) {
FOR(j, 1, k + 1) {
int nx = now.first + j * dx[i], ny = now.second + j * dy[i];
if (not isin(nx, ny) or c[nx][ny] == '@' or
d[nx][ny] <= d[now.first][now.second])
break;
if (d[nx][ny] == inf) {
d[nx][ny] = d[now.first][now.second] + 1;
q.push(pii(nx, ny));
}
}
}
}
if (d[gx][gy] == inf)
cout << -1 << endl;
else
cout << d[gx][gy] << endl;
} | replace | 60 | 61 | 60 | 62 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back
// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N) \
cerr << #A << " : ["; \
for (int i = 0; i < N; i++) \
cerr << A[i] << " "; \
cerr << "]\n";
#define debuga2(A, N, M) \
cerr << #A << " : \n"; \
for (int i = 0; i < N; i++) { \
cerr << "["; \
for (int j = 0; j < M; ++j) \
cerr << A[i][j] << " "; \
cerr << "]\n"; \
}
#define debugp(p) \
cerr << #p << " : " \
<< "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v) \
cerr << #v << " : " \
<< "["; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "]\n";
#define debugv2(v) \
cerr << #v << " : \n"; \
for (int i = 0; i < v.size(); i++) { \
cerr << "["; \
for (int j = 0; j < (v[0].size()); ++j) \
cerr << v[i][j] << " "; \
cerr << "]\n"; \
}
#define debugs(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << *itr << " "; \
cerr << "]\n";
#define debugm(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << "(" << itr->first << "," << itr->second << ") "; \
cerr << "]\n";
// dijstra's algo
void solve() {
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int h, w, k;
cin >> h >> w >> k;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
--sx;
--sy;
--tx;
--ty;
vector<string> s(h);
forn(i, 0, h) cin >> s[i];
queue<pair<int, pair<int, int>>> q;
q.push({0, {sx, sy}});
int dist[h][w];
memset(dist, inf, sizeof(dist));
dist[sx][sy] = 0;
while (q.size() > 0) {
auto temp = q.front();
q.pop();
int cost = temp.first;
int x = temp.second.first, y = temp.second.second;
forn(i, 0, 4) {
forn(j, 1, k + 1) {
int nx = x + dx[i] * j, ny = y + dy[i] * j;
if (nx < 0 || ny < 0 || nx >= h || ny >= w)
break;
int cur_cost = cost + 1;
if (s[nx][ny] == '@')
break;
if (dist[nx][ny] > cur_cost) {
dist[nx][ny] = cur_cost;
q.push({cur_cost, {nx, ny}});
}
}
}
}
cout << (dist[tx][ty] == inf ? -1 : dist[tx][ty]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back
// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N) \
cerr << #A << " : ["; \
for (int i = 0; i < N; i++) \
cerr << A[i] << " "; \
cerr << "]\n";
#define debuga2(A, N, M) \
cerr << #A << " : \n"; \
for (int i = 0; i < N; i++) { \
cerr << "["; \
for (int j = 0; j < M; ++j) \
cerr << A[i][j] << " "; \
cerr << "]\n"; \
}
#define debugp(p) \
cerr << #p << " : " \
<< "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v) \
cerr << #v << " : " \
<< "["; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "]\n";
#define debugv2(v) \
cerr << #v << " : \n"; \
for (int i = 0; i < v.size(); i++) { \
cerr << "["; \
for (int j = 0; j < (v[0].size()); ++j) \
cerr << v[i][j] << " "; \
cerr << "]\n"; \
}
#define debugs(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << *itr << " "; \
cerr << "]\n";
#define debugm(m) \
cerr << #m << " : [ "; \
for (auto itr = m.begin(); itr != m.end(); itr++) \
cerr << "(" << itr->first << "," << itr->second << ") "; \
cerr << "]\n";
// dijstra's algo
void solve() {
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int h, w, k;
cin >> h >> w >> k;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
--sx;
--sy;
--tx;
--ty;
vector<string> s(h);
forn(i, 0, h) cin >> s[i];
queue<pair<int, pair<int, int>>> q;
q.push({0, {sx, sy}});
int dist[h][w];
memset(dist, inf, sizeof(dist));
dist[sx][sy] = 0;
while (q.size() > 0) {
auto temp = q.front();
q.pop();
int cost = temp.first;
int x = temp.second.first, y = temp.second.second;
forn(i, 0, 4) {
forn(j, 1, k + 1) {
int nx = x + dx[i] * j, ny = y + dy[i] * j;
if (nx < 0 || ny < 0 || nx >= h || ny >= w)
break;
int cur_cost = cost + 1;
if (s[nx][ny] == '@')
break;
if (dist[nx][ny] > cur_cost) {
dist[nx][ny] = cur_cost;
q.push({cur_cost, {nx, ny}});
}
if (dist[nx][ny] < cur_cost)
break;
}
}
}
cout << (dist[tx][ty] == inf ? -1 : dist[tx][ty]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| insert | 89 | 89 | 89 | 91 | TLE | |
p02644 | Python | Time Limit Exceeded | from collections import deque
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
x2 -= 1
y1 -= 1
y2 -= 1
# C[h][w]
C = [list(input()) for _ in range(h)]
dist = [[10**10] * w for _ in range(h)]
dist[x1][y1] = 0
que = deque([])
que.append((x1, y1))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while que:
a, b = que.popleft()
d = dist[a][b]
if a == x2 and b == y2:
print(dist[x2][y2])
exit()
for i in range(4):
cx = a + dx[i]
cy = b + dy[i]
if cx < 0 or cx >= h or cy < 0 or cy >= w:
continue
if C[cx][cy] == "@":
continue
for j in range(1, k + 1):
nx = a + dx[i] * j
ny = b + dy[i] * j
if nx < 0 or nx >= h or ny < 0 or ny >= w:
break
if C[nx][ny] == "@":
break
if dist[nx][ny] == 10**10:
dist[nx][ny] = d + 1
que.append((nx, ny))
print(-1)
| from collections import deque
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
x2 -= 1
y1 -= 1
y2 -= 1
# C[h][w]
C = [list(input()) for _ in range(h)]
dist = [[10**10] * w for _ in range(h)]
dist[x1][y1] = 0
que = deque([])
que.append((x1, y1))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
while que:
a, b = que.popleft()
d = dist[a][b]
if a == x2 and b == y2:
print(dist[x2][y2])
exit()
for i in range(4):
cx = a + dx[i]
cy = b + dy[i]
if cx < 0 or cx >= h or cy < 0 or cy >= w:
continue
if C[cx][cy] == "@":
continue
for j in range(1, k + 1):
nx = a + dx[i] * j
ny = b + dy[i] * j
if nx < 0 or nx >= h or ny < 0 or ny >= w:
break
if C[nx][ny] == "@":
break
if dist[nx][ny] < d + 1:
break
if dist[nx][ny] == 10**10:
dist[nx][ny] = d + 1
que.append((nx, ny))
print(-1)
| insert | 40 | 40 | 40 | 42 | TLE | |
p02644 | Python | Time Limit Exceeded | import sys
from collections import deque
def bfs(x1, y1, d):
q = deque([])
q.append((d, x1, y1))
M[x1][y1] = d
while q:
d, x1, y1 = q.popleft()
if [x1, y1] == [xg, yg]:
print(d)
return
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
for k in range(1, K + 1):
x2 = x1 + dx * k
y2 = y1 + dy * k
# if m[x2][y2] == '@':
# break
# if x2 < 0 or x2 >= H:
# break
# if y2 < 0 or y2 >= W:
# break
# if M[x2][y2] < d + 1:
# break
# if M[x2][y2] == d + 1:
# continue
if (0 <= x2 < H) and (0 <= y2 < W):
if m[x2][y2] == "@":
break
elif M[x2][y2] == -1:
M[x2][y2] = d + 1
q.append((d + 1, x2, y2))
elif M[x2][y2] <= d:
break
elif M[x2][y2] == d + 1:
continue
else:
break
print(-1)
H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1
m = []
for _ in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for _ in range(H)]
bfs(xs - 1, ys - 1, 0)
| import sys
from collections import deque
def bfs(x1, y1, d):
q = deque([])
q.append((d, x1, y1))
M[x1][y1] = d
while q:
d, x1, y1 = q.popleft()
if [x1, y1] == [xg, yg]:
print(d)
return
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
for k in range(1, K + 1):
x2 = x1 + dx * k
y2 = y1 + dy * k
# if m[x2][y2] == '@':
# break
# if x2 < 0 or x2 >= H:
# break
# if y2 < 0 or y2 >= W:
# break
# if M[x2][y2] < d + 1:
# break
# if M[x2][y2] == d + 1:
# continue
if (0 <= x2 < H) and (0 <= y2 < W):
if m[x2][y2] == "@":
break
elif M[x2][y2] == -1:
M[x2][y2] = d + 1
q.append((d + 1, x2, y2))
elif M[x2][y2] <= d:
break
elif M[x2][y2] == d + 1:
continue
else:
break
else:
break
print(-1)
H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1
m = []
for _ in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for _ in range(H)]
bfs(xs - 1, ys - 1, 0)
| insert | 43 | 43 | 43 | 45 | TLE | |
p02644 | Python | Runtime Error | from collections import deque
import sys
def bfs(x1, y1, d):
q = deque()
q.append((d, x1, y1))
while q:
d, x1, y1 = q.popleft()
M[x1][y1] = d
if [x1, y1] == [xg, yg]:
return
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
for k in range(1, K + 1):
x2 = x1 + dx * k
y2 = y1 + dy * k
if (0 <= x2 < H) and (0 <= y2 < W):
if m[x2][y2] == "@":
break
elif M[x2][y2] == -1:
M[x2][y2] = d + 1
q.append((x2, y2, d + 1)) # 新しい点を足す。
elif M[x2][y2] < d + 1:
break
else:
break
H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1
m = []
for i in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for i in range(H)]
bfs(xs, ys, 0)
print(M[xg][yg])
| from collections import deque
import sys
def bfs(x1, y1, d):
q = deque()
q.append((d, x1, y1))
while q:
d, x1, y1 = q.popleft()
M[x1][y1] = d
if [x1, y1] == [xg, yg]:
return
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
for k in range(1, K + 1):
x2 = x1 + dx * k
y2 = y1 + dy * k
if (0 <= x2 < H) and (0 <= y2 < W):
if m[x2][y2] == "@":
break
elif M[x2][y2] == -1:
M[x2][y2] = d + 1
q.append((d + 1, x2, y2)) # 新しい点を足す。
elif M[x2][y2] < d + 1:
break
else:
break
H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1
m = []
for i in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for i in range(H)]
bfs(xs, ys, 0)
print(M[xg][yg])
| replace | 26 | 27 | 26 | 27 | 0 | |
p02644 | Python | Time Limit Exceeded | from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10**9
D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])
while que:
nh, nw = que.pop()
for dh, dw in directions:
for k in range(1, K + 1):
nx_h, nx_w = nh + k * dh, nw + k * dw
if not (0 <= nx_h < H and 0 <= nx_w < W):
break
if G[nx_h][nx_w] == "@":
break
if D[nx_h][nx_w] <= D[nh][nw]:
break
D[nx_h][nx_w] = D[nh][nw] + 1
que.appendleft((nx_h, nx_w))
print(D[gh][gw] if D[gh][gw] != INF else -1)
| from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10**9
D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])
while que:
nh, nw = que.pop()
for dh, dw in directions:
for k in range(1, K + 1):
nx_h, nx_w = nh + k * dh, nw + k * dw
if not (0 <= nx_h < H and 0 <= nx_w < W):
break
if G[nx_h][nx_w] == "@":
break
if D[nx_h][nx_w] <= D[nh][nw]:
break
if D[nx_h][nx_w] > D[nh][nw] + 1:
D[nx_h][nx_w] = D[nh][nw] + 1
que.appendleft((nx_h, nx_w))
print(D[gh][gw] if D[gh][gw] != INF else -1)
| replace | 28 | 30 | 28 | 31 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <typename T> using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for (ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for (ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for (ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for (ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ll pow2(const T n) { return (1LL << n); }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void co(void) { cout << '\n'; }
template <typename T> void co(const T n) { cout << n << '\n'; }
template <typename T1, typename T2> void co(pair<T1, T2> p) {
cout << p.fs << ' ' << p.sc << '\n';
}
template <typename T> void co(const vector<T> &v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void co(initializer_list<T> v) {
for (T i : v)
cosp(i);
co();
}
void ce(void) { cerr << '\n'; }
template <typename T> void ce(const T n) { cerr << n << '\n'; }
template <typename T> void cesp(const T n) { cerr << n << ' '; }
template <typename T> void ce(initializer_list<T> v) {
for (T i : v)
cesp(i);
ce();
}
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
constexpr int INF = 1e9 + 1;
constexpr ll LINF = 1e18L + 1;
constexpr ll MOD = 1e9L + 7;
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
ll h, w, k;
VecS board;
Vec2<ll> dist;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
bool in_board(ll x, ll y) {
return x >= 0 && x < h && y >= 0 && y < w && board[x][y] == '.';
}
void bfs(ll fx, ll fy) {
dist[fx][fy] = 0;
queue<P> que;
que.push({fx, fy});
while (!que.empty()) {
P p = que.front();
que.pop();
// co(p);
ll a = p.first, b = p.second;
rep(i, 4) {
ll x = a, y = b;
repn(j, k) {
x += dx[i], y += dy[i];
// co({x, y});
if (in_board(x, y)) {
if (chmin(dist[x][y], dist[a][b] + 1)) {
que.push({x, y});
}
} else
break;
}
}
}
}
int main(void) {
cin >> h >> w >> k;
dist.resize(h);
rep(i, h) dist[i].resize(w, LINF);
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
board.resize(h);
rep(i, h) cin >> board[i];
bfs(x1, y1);
ll ans = dist[x2][y2];
/*
rep(i, h){
rep(j, w) cosp(dist[i][j]);
co();
}
*/
if (ans == LINF)
ans = -1;
co(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <typename T> using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for (ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for (ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for (ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for (ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ll pow2(const T n) { return (1LL << n); }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void co(void) { cout << '\n'; }
template <typename T> void co(const T n) { cout << n << '\n'; }
template <typename T1, typename T2> void co(pair<T1, T2> p) {
cout << p.fs << ' ' << p.sc << '\n';
}
template <typename T> void co(const vector<T> &v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void co(initializer_list<T> v) {
for (T i : v)
cosp(i);
co();
}
void ce(void) { cerr << '\n'; }
template <typename T> void ce(const T n) { cerr << n << '\n'; }
template <typename T> void cesp(const T n) { cerr << n << ' '; }
template <typename T> void ce(initializer_list<T> v) {
for (T i : v)
cesp(i);
ce();
}
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
constexpr int INF = 1e9 + 1;
constexpr ll LINF = 1e18L + 1;
constexpr ll MOD = 1e9L + 7;
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
ll h, w, k;
VecS board;
Vec2<ll> dist;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
bool in_board(ll x, ll y) {
return x >= 0 && x < h && y >= 0 && y < w && board[x][y] == '.';
}
void bfs(ll fx, ll fy) {
dist[fx][fy] = 0;
queue<P> que;
que.push({fx, fy});
while (!que.empty()) {
P p = que.front();
que.pop();
// co(p);
ll a = p.first, b = p.second;
rep(i, 4) {
ll x = a, y = b;
repn(j, k) {
x += dx[i], y += dy[i];
// co({x, y});
if (in_board(x, y)) {
if (chmin(dist[x][y], dist[a][b] + 1)) {
que.push({x, y});
} else if (dist[x][y] == dist[a][b] + 1) {
continue;
} else
break;
} else
break;
}
}
}
}
int main(void) {
cin >> h >> w >> k;
dist.resize(h);
rep(i, h) dist[i].resize(w, LINF);
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
board.resize(h);
rep(i, h) cin >> board[i];
bfs(x1, y1);
ll ans = dist[x2][y2];
/*
rep(i, h){
rep(j, w) cosp(dist[i][j]);
co();
}
*/
if (ans == LINF)
ans = -1;
co(ans);
return 0;
}
| replace | 103 | 104 | 103 | 107 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *std::min_element((a).begin(), (a).end())
#define MAX(a) *std::max_element((a).begin(), (a).end())
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0LL)
#define REP(i, n) for (std::size_t(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (std::size_t(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (std::size_t(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (std::size_t(i) = (n)-1; (i) >= (m); i--)
#define debug(x) std::cerr << #x << " = " << x << '\n'
// #define int long long
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vl = std::vector<long long>;
using vvl = std::vector<std::vector<long long>>;
// using bint = boost::multiprecision::cpp_int;
// using rat = boost::rational<bint>;
constexpr int inf = (1 << 30) - 1;
constexpr long long INF = 1LL << 60;
inline void ios_() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
}
template <typename T> inline T Div(const T &a, const T &b) {
return (a + b - 1) / b;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline void finish(const T &msg) {
std::cout << msg << std::endl;
std::exit(0);
}
template <typename T>
long long binarySearch(long long ng, long long ok, const T &fn) {
while (std::abs(ok - ng) > 1) {
T mid = (ok + ng) / 2;
if (fn(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
struct ModInt {
long long value;
static const long long mod; // mod must be a prime number
ModInt(long long v = 0) : value(((v % mod) + mod) % mod) {}
ModInt pow(long long n) const {
long long x = value, res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return ModInt(res);
}
ModInt inverse() const { return pow(mod - 2); }
ModInt operator-() const { return ModInt((mod - value) % mod); }
ModInt &operator+=(const ModInt &a) {
value += a.value;
value %= mod;
return *this;
}
ModInt &operator-=(const ModInt &a) {
value += (-a).value;
value %= mod;
return *this;
}
ModInt &operator*=(const ModInt &a) {
value *= a.value;
value %= mod;
return *this;
}
ModInt &operator/=(const ModInt &a) { return (*this) *= a.inverse(); }
ModInt operator+(const ModInt &a) const { return ModInt(*this) += a; }
ModInt operator-(const ModInt &a) const { return ModInt(*this) -= a; }
ModInt operator*(const ModInt &a) const { return ModInt(*this) *= a; }
ModInt operator/(const ModInt &a) const { return ModInt(*this) /= a; }
bool operator==(const ModInt &a) const { return value == a.value; }
bool operator!=(const ModInt &a) const { return value != a.value; }
friend std::istream &operator>>(std::istream &is, ModInt &a) {
long long v;
is >> v;
a = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) {
os << a.value;
return os;
}
static ModInt factorial(int n) {
static std::vector<ModInt> fact(1 << 18);
if (fact.size() <= n) {
fact.resize(n + 1);
}
if (n < 0) {
return 0;
}
if (n == 0) {
return 1;
}
if (fact[n] != 0) {
return fact[n];
}
return fact[n] = factorial(n - 1) * n;
}
static ModInt combin(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return 0;
}
return factorial(n) / (factorial(k) * factorial(n - k));
}
};
const long long ModInt::mod = 1e9 + 7;
// const long long ModInt::mod = 998244353;
using mint = ModInt;
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
using ll = long long;
struct Graph {
struct Edge {
int from;
int to;
ll cost;
int dx, dy;
Edge(int a, ll b) : to(a), cost(b) {}
Edge(int a, int b, ll c) : from(a), to(b), cost(c) {}
Edge(int a, int b, ll c, int x, int y)
: from(a), to(b), cost(c), dx(x), dy(y) {}
};
int N; // 頂点数
std::vector<std::vector<Edge>> edges; // 隣接リスト
std::vector<ll> dist; // ノードの深さ
std::vector<int> deg; // 入次数
Graph(int n) : N(n), edges(n), dist(n, INF), deg(n, 0) {}
void connect(int a, int b, ll c) { // 辺を追加
edges[a].emplace_back(Edge(a, b, c));
deg[b]++;
}
bool visited(int v) { return dist[v] < INF; }
void resetDist() {
for (int i = 0; i < N; i++) {
dist[i] = INF;
}
}
void dfs(int p, int v, ll d) { // 深さ優先探索
if (visited(v))
return;
dist[v] = d;
for (const Edge &e : edges[v]) {
if (e.to == p)
continue;
dfs(v, e.to, d + e.cost);
}
}
void bfs(int v) { // 幅優先探索
std::queue<Edge> que;
for (const Edge &e : edges[v]) {
que.push(e);
dist[e.to] = e.cost;
}
dist[v] = 0;
while (!que.empty()) {
int u = que.front().to;
ll d = que.front().cost;
que.pop();
for (const Edge &e : edges[u]) {
if (!visited(e.to)) {
que.push(Edge(e.from, e.to, d + e.cost));
dist[e.to] = d + e.cost;
}
}
}
}
ll diameter() { // 木の直径を求める
static ll res = INF;
if (res != INF)
return res; // 計算済みの場合
dfs(-1, 0, 0); // 頂点0からの最遠頂点vを求める
int v = std::max_element(dist.begin(), dist.end()) - dist.begin();
resetDist();
dfs(-1, v, 0); // vからの最遠頂点を求める
return res = *std::max_element(dist.begin(),
dist.end()); // この2頂点間の距離が直径
}
void dijkstra(int start) { // ダイクストラ法
dist[start] = 0;
auto fn = [](const Edge &a, const Edge &b) { return a.cost > b.cost; };
std::priority_queue<Edge, std::vector<Edge>, decltype(fn)> que(fn);
for (const Edge &e : edges[start])
que.push(e);
while (!que.empty()) {
auto v = que.top().to;
auto d = que.top().cost;
que.pop();
if (visited(v))
continue;
else
dist[v] = d;
for (const Edge &e : edges[v]) {
if (dist[e.to] == INF) {
que.push(Edge(e.from, e.to, e.cost + d));
}
}
}
}
};
struct MatrixGraph : Graph {
MatrixGraph(int n) : Graph(n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
edges[i].emplace_back(Edge(i, j, (i == j ? 0 : INF)));
}
}
}
void connect(int a, int b, ll c) { edges[a][b].cost = c; }
void WarshallFloyd() {
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ll d = edges[i][k].cost + edges[k][j].cost;
if (d < edges[i][j].cost) {
edges[i][j].cost = d;
}
}
}
}
}
};
struct GridGraph : Graph {
int H, W;
std::vector<std::vector<bool>> grid;
std::vector<int> dx, dy;
int K;
GridGraph(int h, int w)
: Graph(h * w), H(h), W(w), grid(h, std::vector<bool>(w, false)) {
dx = {1, 0, -1, 0, 1, 1, -1, -1};
dy = {0, 1, 0, -1, 1, -1, 1, -1};
}
void setGrid(int x, int y, bool b) { grid[x][y] = b; }
bool out(int x, int y) { return (x < 0 || x >= H || y < 0 || y >= W); }
int to1D(int x, int y) { return x * W + y; }
int getx(int i) { return i / W; }
int gety(int i) { return i % W; }
void connectAll(bool diagonal) {
for (int x = 0; x < H; x++) {
for (int y = 0; y < W; y++) {
if (!grid[x][y])
continue;
for (int k = 0; k < (diagonal ? 8 : 4); k++) {
int nx = x + dx[k], ny = y + dy[k];
if (out(nx, ny) || !grid[nx][ny])
continue;
connect(to1D(x, y), to1D(nx, ny), 1);
}
}
}
}
void bfs(int v) { // 幅優先探索
queue<Edge> que;
que.push(Edge(-1, v, 0));
dist[v] = 0;
while (!que.empty()) {
auto e = que.front();
int u = e.to;
ll d = e.cost;
que.pop();
REP(i, 4) {
int x = getx(u), y = gety(u);
int ddx = 0, ddy = 0;
REP(j, K) {
ddx += dx[i];
ddy += dy[i];
if (out(x + ddx, y + ddy) || !grid[x + ddx][y + ddy])
break;
int next = to1D(x + ddx, y + ddy);
if (visited(next))
continue;
que.push(Edge(u, next, d + 1));
chmin(dist[next], d + 1);
}
}
}
}
};
signed main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
GridGraph G(h, w);
REP(i, h) {
REP(j, w) {
char c;
cin >> c;
G.setGrid(i, j, (c == '.'));
}
}
G.K = k;
G.bfs(G.to1D(x1, y1));
ll ans = G.dist[G.to1D(x2, y2)];
cout << (ans < INF ? ans : -1) << endl;
return 0;
}
| #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *std::min_element((a).begin(), (a).end())
#define MAX(a) *std::max_element((a).begin(), (a).end())
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0LL)
#define REP(i, n) for (std::size_t(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (std::size_t(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (std::size_t(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (std::size_t(i) = (n)-1; (i) >= (m); i--)
#define debug(x) std::cerr << #x << " = " << x << '\n'
// #define int long long
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vl = std::vector<long long>;
using vvl = std::vector<std::vector<long long>>;
// using bint = boost::multiprecision::cpp_int;
// using rat = boost::rational<bint>;
constexpr int inf = (1 << 30) - 1;
constexpr long long INF = 1LL << 60;
inline void ios_() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
}
template <typename T> inline T Div(const T &a, const T &b) {
return (a + b - 1) / b;
}
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> inline void finish(const T &msg) {
std::cout << msg << std::endl;
std::exit(0);
}
template <typename T>
long long binarySearch(long long ng, long long ok, const T &fn) {
while (std::abs(ok - ng) > 1) {
T mid = (ok + ng) / 2;
if (fn(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
struct ModInt {
long long value;
static const long long mod; // mod must be a prime number
ModInt(long long v = 0) : value(((v % mod) + mod) % mod) {}
ModInt pow(long long n) const {
long long x = value, res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return ModInt(res);
}
ModInt inverse() const { return pow(mod - 2); }
ModInt operator-() const { return ModInt((mod - value) % mod); }
ModInt &operator+=(const ModInt &a) {
value += a.value;
value %= mod;
return *this;
}
ModInt &operator-=(const ModInt &a) {
value += (-a).value;
value %= mod;
return *this;
}
ModInt &operator*=(const ModInt &a) {
value *= a.value;
value %= mod;
return *this;
}
ModInt &operator/=(const ModInt &a) { return (*this) *= a.inverse(); }
ModInt operator+(const ModInt &a) const { return ModInt(*this) += a; }
ModInt operator-(const ModInt &a) const { return ModInt(*this) -= a; }
ModInt operator*(const ModInt &a) const { return ModInt(*this) *= a; }
ModInt operator/(const ModInt &a) const { return ModInt(*this) /= a; }
bool operator==(const ModInt &a) const { return value == a.value; }
bool operator!=(const ModInt &a) const { return value != a.value; }
friend std::istream &operator>>(std::istream &is, ModInt &a) {
long long v;
is >> v;
a = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) {
os << a.value;
return os;
}
static ModInt factorial(int n) {
static std::vector<ModInt> fact(1 << 18);
if (fact.size() <= n) {
fact.resize(n + 1);
}
if (n < 0) {
return 0;
}
if (n == 0) {
return 1;
}
if (fact[n] != 0) {
return fact[n];
}
return fact[n] = factorial(n - 1) * n;
}
static ModInt combin(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return 0;
}
return factorial(n) / (factorial(k) * factorial(n - k));
}
};
const long long ModInt::mod = 1e9 + 7;
// const long long ModInt::mod = 998244353;
using mint = ModInt;
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
using ll = long long;
struct Graph {
struct Edge {
int from;
int to;
ll cost;
int dx, dy;
Edge(int a, ll b) : to(a), cost(b) {}
Edge(int a, int b, ll c) : from(a), to(b), cost(c) {}
Edge(int a, int b, ll c, int x, int y)
: from(a), to(b), cost(c), dx(x), dy(y) {}
};
int N; // 頂点数
std::vector<std::vector<Edge>> edges; // 隣接リスト
std::vector<ll> dist; // ノードの深さ
std::vector<int> deg; // 入次数
Graph(int n) : N(n), edges(n), dist(n, INF), deg(n, 0) {}
void connect(int a, int b, ll c) { // 辺を追加
edges[a].emplace_back(Edge(a, b, c));
deg[b]++;
}
bool visited(int v) { return dist[v] < INF; }
void resetDist() {
for (int i = 0; i < N; i++) {
dist[i] = INF;
}
}
void dfs(int p, int v, ll d) { // 深さ優先探索
if (visited(v))
return;
dist[v] = d;
for (const Edge &e : edges[v]) {
if (e.to == p)
continue;
dfs(v, e.to, d + e.cost);
}
}
void bfs(int v) { // 幅優先探索
std::queue<Edge> que;
for (const Edge &e : edges[v]) {
que.push(e);
dist[e.to] = e.cost;
}
dist[v] = 0;
while (!que.empty()) {
int u = que.front().to;
ll d = que.front().cost;
que.pop();
for (const Edge &e : edges[u]) {
if (!visited(e.to)) {
que.push(Edge(e.from, e.to, d + e.cost));
dist[e.to] = d + e.cost;
}
}
}
}
ll diameter() { // 木の直径を求める
static ll res = INF;
if (res != INF)
return res; // 計算済みの場合
dfs(-1, 0, 0); // 頂点0からの最遠頂点vを求める
int v = std::max_element(dist.begin(), dist.end()) - dist.begin();
resetDist();
dfs(-1, v, 0); // vからの最遠頂点を求める
return res = *std::max_element(dist.begin(),
dist.end()); // この2頂点間の距離が直径
}
void dijkstra(int start) { // ダイクストラ法
dist[start] = 0;
auto fn = [](const Edge &a, const Edge &b) { return a.cost > b.cost; };
std::priority_queue<Edge, std::vector<Edge>, decltype(fn)> que(fn);
for (const Edge &e : edges[start])
que.push(e);
while (!que.empty()) {
auto v = que.top().to;
auto d = que.top().cost;
que.pop();
if (visited(v))
continue;
else
dist[v] = d;
for (const Edge &e : edges[v]) {
if (dist[e.to] == INF) {
que.push(Edge(e.from, e.to, e.cost + d));
}
}
}
}
};
struct MatrixGraph : Graph {
MatrixGraph(int n) : Graph(n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
edges[i].emplace_back(Edge(i, j, (i == j ? 0 : INF)));
}
}
}
void connect(int a, int b, ll c) { edges[a][b].cost = c; }
void WarshallFloyd() {
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ll d = edges[i][k].cost + edges[k][j].cost;
if (d < edges[i][j].cost) {
edges[i][j].cost = d;
}
}
}
}
}
};
struct GridGraph : Graph {
int H, W;
std::vector<std::vector<bool>> grid;
std::vector<int> dx, dy;
int K;
GridGraph(int h, int w)
: Graph(h * w), H(h), W(w), grid(h, std::vector<bool>(w, false)) {
dx = {1, 0, -1, 0, 1, 1, -1, -1};
dy = {0, 1, 0, -1, 1, -1, 1, -1};
}
void setGrid(int x, int y, bool b) { grid[x][y] = b; }
bool out(int x, int y) { return (x < 0 || x >= H || y < 0 || y >= W); }
int to1D(int x, int y) { return x * W + y; }
int getx(int i) { return i / W; }
int gety(int i) { return i % W; }
void connectAll(bool diagonal) {
for (int x = 0; x < H; x++) {
for (int y = 0; y < W; y++) {
if (!grid[x][y])
continue;
for (int k = 0; k < (diagonal ? 8 : 4); k++) {
int nx = x + dx[k], ny = y + dy[k];
if (out(nx, ny) || !grid[nx][ny])
continue;
connect(to1D(x, y), to1D(nx, ny), 1);
}
}
}
}
void bfs(int v) { // 幅優先探索
queue<Edge> que;
que.push(Edge(-1, v, 0));
dist[v] = 0;
while (!que.empty()) {
auto e = que.front();
int u = e.to;
ll d = e.cost;
que.pop();
REP(i, 4) {
int x = getx(u), y = gety(u);
int ddx = 0, ddy = 0;
REP(j, K) {
ddx += dx[i];
ddy += dy[i];
if (out(x + ddx, y + ddy) || !grid[x + ddx][y + ddy])
break;
int next = to1D(x + ddx, y + ddy);
if (dist[u] >= dist[next])
break;
if (visited(next))
continue;
que.push(Edge(u, next, d + 1));
chmin(dist[next], d + 1);
}
}
}
}
};
signed main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
GridGraph G(h, w);
REP(i, h) {
REP(j, w) {
char c;
cin >> c;
G.setGrid(i, j, (c == '.'));
}
}
G.K = k;
G.bfs(G.to1D(x1, y1));
ll ans = G.dist[G.to1D(x2, y2)];
cout << (ans < INF ? ans : -1) << endl;
return 0;
}
| insert | 362 | 362 | 362 | 364 | TLE | |
p02644 | C++ | Runtime Error | #include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define ll long long int
#define oo 1000000000000000000
#define forr(i, n) for (ll i = 0; i < n; i++)
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define pb push_back
using namespace std;
const ll MOD = 1e9 + 7;
ll f[1000100];
pair<int, int> dxy[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void __sol() {
int n, m, k;
cin >> n >> m >> k;
int x, y, go, to;
cin >> x >> y >> go >> to;
vector<string> v(n + 3);
forr(i, n) cin >> v[i + 1], v[i + 1] = '@' + v[i + 1] + '@';
v[0] = v[1];
v[n + 1] = v[0];
// x-- , y-- , go--,to--;
int d[n + 1][m + 1];
// memset(d,-1,sizeof(d));
forr(i, n + 1) forr(j, m + 1) d[i][j] = n * m;
d[x][y] = 0;
queue<pair<int, int>> qq;
qq.push({x, y});
while (!qq.empty()) {
pair<int, int> p = qq.front();
qq.pop();
x = p.first, y = p.second; // cout << x <<" " << y << "\n";
if (x == go && y == to) {
cout << d[go][to];
return;
}
for (auto &ooo : dxy) {
int p = ooo.first, q = ooo.second;
for (int i = 1; i <= k; i++) {
// if( x+i*p<n && x+i*p>=0 && y+i*q<m && y+i*q>=0){
if (v[x + i * p][y + i * q] != '.' ||
d[x + i * p][y + i * q] <= d[x][y])
break;
if (d[x + i * p][y + i * q] > 1 + d[x][y]) {
d[x + i * p][y + i * q] = 1 + d[x][y];
qq.push({x + i * p, y + i * q});
}
}
}
}
if (d[go][to] != n * m)
cout << d[go][to];
else
cout << -1;
}
int main() {
fastio ll tc = 1; // cin >> tc;
while (tc--)
__sol();
return 0;
}
| #include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define ll long long int
#define oo 1000000000000000000
#define forr(i, n) for (ll i = 0; i < n; i++)
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define pb push_back
using namespace std;
const ll MOD = 1e9 + 7;
ll f[1000100];
pair<int, int> dxy[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void __sol() {
int n, m, k;
cin >> n >> m >> k;
int x, y, go, to;
cin >> x >> y >> go >> to;
vector<string> v(n + 3);
forr(i, n) cin >> v[i + 1], v[i + 1] = '@' + v[i + 1] + '@';
string faltu(m + 3, '@');
v[0] = faltu;
v[n + 1] = faltu;
// x-- , y-- , go--,to--;
int d[n + 1][m + 1];
// memset(d,-1,sizeof(d));
forr(i, n + 1) forr(j, m + 1) d[i][j] = n * m;
d[x][y] = 0;
queue<pair<int, int>> qq;
qq.push({x, y});
while (!qq.empty()) {
pair<int, int> p = qq.front();
qq.pop();
x = p.first, y = p.second; // cout << x <<" " << y << "\n";
if (x == go && y == to) {
cout << d[go][to];
return;
}
for (auto &ooo : dxy) {
int p = ooo.first, q = ooo.second;
for (int i = 1; i <= k; i++) {
// if( x+i*p<n && x+i*p>=0 && y+i*q<m && y+i*q>=0){
if (v[x + i * p][y + i * q] != '.' ||
d[x + i * p][y + i * q] <= d[x][y])
break;
if (d[x + i * p][y + i * q] > 1 + d[x][y]) {
d[x + i * p][y + i * q] = 1 + d[x][y];
qq.push({x + i * p, y + i * q});
}
}
}
}
if (d[go][to] != n * m)
cout << d[go][to];
else
cout << -1;
}
int main() {
fastio ll tc = 1; // cin >> tc;
while (tc--)
__sol();
return 0;
}
| replace | 31 | 33 | 31 | 34 | -11 | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';
const int mx = 200010;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> c(h);
rep(i, h) { cin >> c[i]; }
vector<vector<int>> d(h, vector<int>(w, inf));
queue<P> q;
q.push(P(sx, sy));
d[sx][sy] = 0;
c[sx][sy] = '@';
while (!q.empty()) {
auto p = q.front();
q.pop();
c[p.F][p.S] = '@';
if (p == P(gx, gy)) {
break;
}
rep(i, 4) {
REP(j, 1, k + 1) {
int nx = p.F + dx[i] * j, ny = p.S + dy[i] * j;
if (nx < 0 || h <= nx || ny < 0 || w <= ny)
break;
if (c[nx][ny] == '@') {
break;
}
if (d[nx][ny] <= d[p.F][p.S])
break;
if (d[nx][ny] < d[p.F][p.S] + 1)
continue;
d[nx][ny] = d[p.F][p.S] + 1;
q.push(P(nx, ny));
}
}
}
int ans = d[gx][gy];
if (ans == inf)
ans = -1;
cout << ans << ln;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';
const int mx = 200010;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
int h, w, k;
cin >> h >> w >> k;
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
sx--;
sy--;
gx--;
gy--;
vector<string> c(h);
rep(i, h) { cin >> c[i]; }
vector<vector<int>> d(h, vector<int>(w, inf));
queue<P> q;
q.push(P(sx, sy));
d[sx][sy] = 0;
c[sx][sy] = '@';
while (!q.empty()) {
auto p = q.front();
q.pop();
c[p.F][p.S] = '@';
if (p == P(gx, gy)) {
break;
}
rep(i, 4) {
REP(j, 1, k + 1) {
int nx = p.F + dx[i] * j, ny = p.S + dy[i] * j;
if (nx < 0 || h <= nx || ny < 0 || w <= ny)
break;
if (c[nx][ny] == '@') {
break;
}
if (d[nx][ny] <= d[p.F][p.S])
break;
if (d[nx][ny] > d[p.F][p.S] + 1) {
d[nx][ny] = d[p.F][p.S] + 1;
q.push(P(nx, ny));
}
}
}
}
int ans = d[gx][gy];
if (ans == inf)
ans = -1;
cout << ans << ln;
return 0;
} | replace | 53 | 58 | 53 | 57 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
// int main() {
// ios::sync_with_stdio(false);
// cin.tie(0);
//
// return 0;
// }
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;
int h, w, k;
vector<vector<char>> MP;
vector<vector<int>> dis;
bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }
bool bad(int x, int y, int nd) { return MP[x][y] == '@'; }
void bfs(int x1, int y1) {
queue<pair<int, int>> que;
que.push({x1, y1});
dis[x1][y1] = 0;
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x, ny = y;
int nd = dis[x][y] + 1;
for (int j = 1; j <= k; j++) {
nx += dir[i][0];
ny += dir[i][1];
if (outside(nx, ny) or bad(nx, ny, nd))
break;
if (dis[nx][ny] > nd) {
que.push({nx, ny});
dis[nx][ny] = nd;
}
}
}
}
}
int main() {
int x1, y1, x2, y2;
cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
MP.resize(h, vector<char>(w));
dis.resize(h, vector<int>(w));
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> MP[i][j], dis[i][j] = INF;
bfs(x1, y1);
cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]);
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
// int main() {
// ios::sync_with_stdio(false);
// cin.tie(0);
//
// return 0;
// }
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;
int h, w, k;
vector<vector<char>> MP;
vector<vector<int>> dis;
bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }
bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }
void bfs(int x1, int y1) {
queue<pair<int, int>> que;
que.push({x1, y1});
dis[x1][y1] = 0;
while (!que.empty()) {
int x = que.front().first;
int y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int nx = x, ny = y;
int nd = dis[x][y] + 1;
for (int j = 1; j <= k; j++) {
nx += dir[i][0];
ny += dir[i][1];
if (outside(nx, ny) or bad(nx, ny, nd))
break;
if (dis[nx][ny] > nd) {
que.push({nx, ny});
dis[nx][ny] = nd;
}
}
}
}
}
int main() {
int x1, y1, x2, y2;
cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
--x1, --y1, --x2, --y2;
MP.resize(h, vector<char>(w));
dis.resize(h, vector<int>(w));
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> MP[i][j], dis[i][j] = INF;
bfs(x1, y1);
cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]);
} | replace | 28 | 29 | 28 | 29 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
using namespace std;
const LL MOD = 1e9 + 7;
LL n, m, t, x1, yy1, x2, y2, dist[1000010], vis[1000010][4],
dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[1000010];
queue<pii> q;
bool out(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; }
int main() {
cin >> n >> m >> t >> x1 >> yy1 >> x2 >> y2;
x1--;
yy1--;
x2--;
y2--;
rep(i, n) cin >> s[i];
rep(i, 1000010) dist[i] = 1e18;
dist[x1 * m + yy1] = 0;
rep(i, 4) {
q.push(mpr(x1 * m + yy1, i));
// vis[x1*m+yy1][i]=1;
}
while (!q.empty()) {
pii p = q.front();
q.pop();
vis[p.fi][p.se] = 1;
int y = p.fi % m, x = p.fi / m;
rep(i, t) {
x += dx[p.se];
y += dy[p.se];
if (out(x, y) || s[x][y] == '@' || vis[x * m + y][p.se] == 1 ||
dist[x * m + y] < dist[p.fi] + 1)
break;
dist[x * m + y] = dist[p.fi] + 1;
// cout<<x<<' '<<y<<' '<<dist[x*m+y]<<endl;
rep(j, 4) {
if (vis[x * m + y][j] == 1)
continue;
q.push(mpr(x * m + y, j));
}
}
}
if (dist[x2 * m + y2] == 1e18)
puts("-1");
else
cout << dist[x2 * m + y2] << endl; /*
rep(i,n)
{
rep(j,m) cout<<dist[i*m+j]<<'\t';cout<<endl;
}*/
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
using namespace std;
const LL MOD = 1e9 + 7;
LL n, m, t, x1, yy1, x2, y2, dist[1000010], vis[1000010][4],
dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[1000010];
queue<pii> q;
bool out(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; }
int main() {
cin >> n >> m >> t >> x1 >> yy1 >> x2 >> y2;
x1--;
yy1--;
x2--;
y2--;
rep(i, n) cin >> s[i];
rep(i, 1000010) dist[i] = 1e18;
dist[x1 * m + yy1] = 0;
rep(i, 4) {
q.push(mpr(x1 * m + yy1, i));
// vis[x1*m+yy1][i]=1;
}
while (!q.empty()) {
pii p = q.front();
q.pop();
if (vis[p.fi][p.se] == 1)
continue;
vis[p.fi][p.se] = 1;
int y = p.fi % m, x = p.fi / m;
rep(i, t) {
x += dx[p.se];
y += dy[p.se];
if (out(x, y) || s[x][y] == '@' || vis[x * m + y][p.se] == 1 ||
dist[x * m + y] < dist[p.fi] + 1)
break;
dist[x * m + y] = dist[p.fi] + 1;
// cout<<x<<' '<<y<<' '<<dist[x*m+y]<<endl;
rep(j, 4) {
if (vis[x * m + y][j] == 1)
continue;
q.push(mpr(x * m + y, j));
}
}
}
if (dist[x2 * m + y2] == 1e18)
puts("-1");
else
cout << dist[x2 * m + y2] << endl; /*
rep(i,n)
{
rep(j,m) cout<<dist[i*m+j]<<'\t';cout<<endl;
}*/
return 0;
} | insert | 38 | 38 | 38 | 40 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<int, int>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int INF = 1e9;
int main() {
int h, w, k;
cin >> h >> w >> k;
int a, b, gx, gy;
cin >> a >> b >> gx >> gy;
a--;
b--;
gx--;
gy--;
string s[h];
rep(i, h) cin >> s[i];
vector<vector<int>> d(h, vector<int>(w, INF));
d[a][b] = 0;
queue<P> que;
que.push(P(a, b));
while (!que.empty()) {
P p = que.front();
que.pop();
int x = p.first, y = p.second;
rep(dir, 4) {
for (int step = 1; step <= k; step++) {
int nx = x + step * dx[dir], ny = y + step * dy[dir];
if (nx < 0 or ny < 0 or nx >= h or ny >= w or s[nx][ny] == '@' or
d[nx][ny] < d[x][y] + 1)
break;
else {
d[nx][ny] = d[x][y] + 1;
que.push(P(nx, ny));
}
}
}
}
cout << (d[gx][gy] == INF ? -1 : d[gx][gy]) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<int, int>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int INF = 1e9;
int main() {
int h, w, k;
cin >> h >> w >> k;
int a, b, gx, gy;
cin >> a >> b >> gx >> gy;
a--;
b--;
gx--;
gy--;
string s[h];
rep(i, h) cin >> s[i];
vector<vector<int>> d(h, vector<int>(w, INF));
d[a][b] = 0;
queue<P> que;
que.push(P(a, b));
while (!que.empty()) {
P p = que.front();
que.pop();
int x = p.first, y = p.second;
rep(dir, 4) {
for (int step = 1; step <= k; step++) {
int nx = x + step * dx[dir], ny = y + step * dy[dir];
if (nx < 0 or ny < 0 or nx >= h or ny >= w or s[nx][ny] == '@' or
d[nx][ny] < d[x][y] + 1)
break;
else {
if (d[nx][ny] > d[x][y] + 1) {
d[nx][ny] = d[x][y] + 1;
que.push(P(nx, ny));
}
}
}
}
}
cout << (d[gx][gy] == INF ? -1 : d[gx][gy]) << endl;
return 0;
}
| replace | 35 | 37 | 35 | 39 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 123456789
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005
vector<vector<int>> a, mark;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tc, i, j, x1, y1, k, cas = 0, q, m, n, x2, y2;
while (cin >> n >> m >> k) {
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
a.resize(n + 1);
mark.resize(n + 1);
for (i = 0; i < n; i++) {
a[i].resize(m + 1);
mark[i].resize(m + 1);
for (j = 0; j < m; j++) {
mark[i][j] = inf;
char ch;
cin >> ch;
if (ch == '.')
a[i][j] = 0;
else
a[i][j] = 1;
}
}
queue<ll> q;
mark[x1][y1] = 0;
q.push(x1);
q.push(y1);
ll ans = inf;
ll cn = 0;
while (q.sz > 0) {
ll ur = q.front();
q.pop();
ll uc = q.front();
q.pop();
cn++;
for (i = 1; i <= k && ur + i < n; i++) {
if (a[ur + i][uc] == 1)
break;
if (mark[ur + i][uc] > mark[ur][uc] + 1) {
mark[ur + i][uc] = mark[ur][uc] + 1;
q.push(ur + i);
q.push(uc);
} else if (mark[ur + i][uc] < mark[ur][uc])
break;
}
for (i = 1; i <= k && ur - i >= 0; i++) {
if (a[ur - i][uc] == 1)
break;
if (mark[ur - i][uc] > mark[ur][uc] + 1) {
mark[ur - i][uc] = mark[ur][uc] + 1;
q.push(ur - i);
q.push(uc);
} else if (mark[ur - i][uc] < mark[ur][uc])
break;
}
for (i = 1; i <= k && uc + i < m; i++) {
if (a[ur][uc + i] == 1)
break;
if (mark[ur][uc + i] > mark[ur][uc] + 1) {
mark[ur][uc + i] = mark[ur][uc] + 1;
q.push(ur);
q.push(uc + i);
} else if (mark[ur][uc + i] < mark[ur][uc])
break;
}
for (i = 1; i <= k && uc - i >= 0; i++) {
if (a[ur][uc - i] == 1)
break;
if (mark[ur][uc - i] > mark[ur][uc] + 1) {
mark[ur][uc - i] = mark[ur][uc] + 1;
q.push(ur);
q.push(uc - i);
} else if (mark[ur][uc - i] < mark[ur][uc])
break;
}
}
ans = mark[x2][y2];
if (ans == inf)
ans = -1;
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 123456789
#define mod 1000000007
#define ull unsigned long long int
#define f(i, k, n) for (i = k; i < n; i++)
#define fr(i, n, k) for (i = n; i >= k; i--)
#define ent(a) scanf("%lld", &a)
#define ent2(a, b) scanf("%lld%lld", &a, &b)
#define ent3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define mem(a) memset(a, 0, sizeof(a))
#define vec(v, s) vector<ll> v[s]
#define arr(a, s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
// knight and king//
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
////////////////////////////
using namespace std;
#define ma 1000005
vector<vector<int>> a, mark;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tc, i, j, x1, y1, k, cas = 0, q, m, n, x2, y2;
while (cin >> n >> m >> k) {
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
a.resize(n + 1);
mark.resize(n + 1);
for (i = 0; i < n; i++) {
a[i].resize(m + 1);
mark[i].resize(m + 1);
for (j = 0; j < m; j++) {
mark[i][j] = inf;
char ch;
cin >> ch;
if (ch == '.')
a[i][j] = 0;
else
a[i][j] = 1;
}
}
queue<ll> q;
mark[x1][y1] = 0;
q.push(x1);
q.push(y1);
ll ans = inf;
ll cn = 0;
while (q.sz > 0) {
if (mark[x2][y2] != inf)
break;
ll ur = q.front();
q.pop();
ll uc = q.front();
q.pop();
cn++;
for (i = 1; i <= k && ur + i < n; i++) {
if (a[ur + i][uc] == 1)
break;
if (mark[ur + i][uc] > mark[ur][uc] + 1) {
mark[ur + i][uc] = mark[ur][uc] + 1;
q.push(ur + i);
q.push(uc);
} else if (mark[ur + i][uc] < mark[ur][uc])
break;
}
for (i = 1; i <= k && ur - i >= 0; i++) {
if (a[ur - i][uc] == 1)
break;
if (mark[ur - i][uc] > mark[ur][uc] + 1) {
mark[ur - i][uc] = mark[ur][uc] + 1;
q.push(ur - i);
q.push(uc);
} else if (mark[ur - i][uc] < mark[ur][uc])
break;
}
for (i = 1; i <= k && uc + i < m; i++) {
if (a[ur][uc + i] == 1)
break;
if (mark[ur][uc + i] > mark[ur][uc] + 1) {
mark[ur][uc + i] = mark[ur][uc] + 1;
q.push(ur);
q.push(uc + i);
} else if (mark[ur][uc + i] < mark[ur][uc])
break;
}
for (i = 1; i <= k && uc - i >= 0; i++) {
if (a[ur][uc - i] == 1)
break;
if (mark[ur][uc - i] > mark[ur][uc] + 1) {
mark[ur][uc - i] = mark[ur][uc] + 1;
q.push(ur);
q.push(uc - i);
} else if (mark[ur][uc - i] < mark[ur][uc])
break;
}
}
ans = mark[x2][y2];
if (ans == inf)
ans = -1;
cout << ans << endl;
}
return 0;
}
| insert | 76 | 76 | 76 | 78 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li h, w, k;
string board[1001000];
li dy[] = {-1, 1, 0, 0};
li dx[] = {0, 0, -1, 1};
int main() {
cin >> h >> w >> k;
PI s, t;
cin >> s.F >> s.S >> t.F >> t.S;
s.F--;
s.S--;
t.F--;
t.S--;
rep(i, h) { cin >> board[i]; }
vector<vector<li>> cost(h, vector<li>(w, INF));
vector<vector<bool>> visited(h, vector<bool>(w, false));
queue<pair<li, PI>> q;
q.push({0, s});
while (!q.empty()) {
li now_cost = q.front().F;
PI now = q.front().S;
q.pop();
if (visited[now.F][now.S]) {
continue;
}
if (now == t) {
cout << now_cost << endl;
return 0;
}
rep(dir, 4) {
repp(len, 1, k + 1) {
PI next = {now.F + dy[dir] * len, now.S + dx[dir] * len};
// p4(now.F, now.S, next.F, next.S);
if (min(next.F, next.S) < 0 || next.F >= h || next.S >= w) {
break;
}
if (board[next.F][next.S] == '@') {
break;
}
if (cost[next.F][next.S] <= now_cost) {
break;
}
cost[next.F][next.S] = now_cost + 1;
q.push({now_cost + 1, next});
}
}
}
puts("-1");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;
#define INF bit(60)
#define DBGP 1
#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\t" << b << endl
#define p3(a, b, c) idp cout << a << "\t" << b << "\t" << c << endl
#define p4(a, b, c, d) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << endl
#define p5(a, b, c, d, e) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << endl
#define p6(a, b, c, d, e, f) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << endl
#define p7(a, b, c, d, e, f, g) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << endl
#define p8(a, b, c, d, e, f, g, h) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j) \
idp cout << a << "\t" << b << "\t" << c << "\t" << d << "\t" << e << "\t" \
<< f << "\t" << g << "\t" << h << "\t" << i << "\t" << j << endl
#define foreach(it, v) \
for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n) \
idp { \
rep(i, n) { cout << x[i] << " "; } \
puts(""); \
}
#define dump2(x, n) \
idp { \
rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; } \
puts(""); \
}
#define dumpi(x) \
idp { \
foreach (it, x) { \
cout << (*it) << " "; \
} \
puts(""); \
}
#define dumpi2(x) \
idp { \
foreach (it, x) { \
cout << "[" << (it)->F << " , " << (it)->S << "] "; \
} \
puts(""); \
}
#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h) \
rep(i, h) { \
rep(j, w) cout << a[i][j] << " "; \
puts(""); \
}
typedef pair<li, li> PI;
li h, w, k;
string board[1001000];
li dy[] = {-1, 1, 0, 0};
li dx[] = {0, 0, -1, 1};
int main() {
cin >> h >> w >> k;
PI s, t;
cin >> s.F >> s.S >> t.F >> t.S;
s.F--;
s.S--;
t.F--;
t.S--;
rep(i, h) { cin >> board[i]; }
vector<vector<li>> cost(h, vector<li>(w, INF));
vector<vector<bool>> visited(h, vector<bool>(w, false));
queue<pair<li, PI>> q;
q.push({0, s});
while (!q.empty()) {
li now_cost = q.front().F;
PI now = q.front().S;
q.pop();
if (visited[now.F][now.S]) {
continue;
}
visited[now.F][now.S] = true;
if (now == t) {
cout << now_cost << endl;
return 0;
}
rep(dir, 4) {
repp(len, 1, k + 1) {
PI next = {now.F + dy[dir] * len, now.S + dx[dir] * len};
// p4(now.F, now.S, next.F, next.S);
if (min(next.F, next.S) < 0 || next.F >= h || next.S >= w) {
break;
}
if (board[next.F][next.S] == '@') {
break;
}
if (cost[next.F][next.S] <= now_cost) {
break;
}
cost[next.F][next.S] = now_cost + 1;
q.push({now_cost + 1, next});
}
}
}
puts("-1");
return 0;
} | insert | 115 | 115 | 115 | 116 | TLE | |
p02644 | C++ | Time Limit Exceeded | //
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, start, end) for (int i = (int)start; i <= (int)end; i++)
#define vrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (int)n; i > 0; i--)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef vector<int> vint;
typedef vector<long> vlong;
const double pi = M_PI;
const int INF10 = 1000000001;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;
struct edge {
edge(int i, long c = 1) {
to = i;
cost = c;
}
int to;
long cost;
};
// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし
// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
int m = 0;
for (m; (1 << m) <= n; m++)
;
m = m - 1;
return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は0)(O(log n))
long myPow(long x, long n, long m = mod) {
if (n < 0)
return 0;
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
bool operator!=(const int a) { return this->x != a; }
bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <class T> void maxin(T &a, T b) { a = max(a, b); }
template <class T> void minin(T &a, T b) { a = min(a, b); }
template <class M, class N> constexpr common_type_t<M, N> gcd(M a, N b) {
a = abs(a);
b = abs(b);
if (a < b)
return gcd(b, a);
M r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
template <class M, class N> constexpr common_type_t<M, N> lcm(M a, N b) {
return a * b / gcd(a, b);
}
const int N_MAX = 1000005;
ll h, w, k;
int sx, sy, ex, ey;
vector<char> s[N_MAX];
ll bfs() {
vector<vector<ll>> d(h + 1, vector<ll>(w + 1, INF18));
queue<pair<int, int>> que;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
d[sx - 1][sy - 1] = 0;
que.push({sx - 1, sy - 1});
while (que.size()) {
pint now = que.front();
que.pop();
if (now.first == ex - 1 && now.second == ey - 1)
break;
rep(i, 4) {
rep1(j, k) {
int nx = now.first + dx[i] * j, ny = now.second + dy[i] * j;
if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != '@') {
if (d[nx][ny] == INF18) {
d[nx][ny] = d[now.first][now.second] + 1;
que.push({nx, ny});
}
} else
break;
}
}
}
return d[ex - 1][ey - 1];
}
int main() {
int x, y;
// 入力
cin >> h >> w >> k;
cin >> sx >> sy >> ex >> ey;
char a;
rep(i, h) {
rep(j, w) {
cin >> a;
s[i].push_back(a);
}
}
// 処理
auto ans = bfs();
// 出力
if (ans == INF18)
cout << -1 << endl;
else
cout << ans << endl;
} | //
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, start, end) for (int i = (int)start; i <= (int)end; i++)
#define vrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (int)n; i > 0; i--)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef vector<int> vint;
typedef vector<long> vlong;
const double pi = M_PI;
const int INF10 = 1000000001;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;
struct edge {
edge(int i, long c = 1) {
to = i;
cost = c;
}
int to;
long cost;
};
// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし
// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
int m = 0;
for (m; (1 << m) <= n; m++)
;
m = m - 1;
return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は0)(O(log n))
long myPow(long x, long n, long m = mod) {
if (n < 0)
return 0;
if (n == 0)
return 1;
if (n % 2 == 0)
return myPow(x * x % m, n / 2, m);
else
return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint &operator+=(const mint a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint pow(ll t) const {
if (!t)
return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1)
a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint &operator/=(const mint a) { return *this *= a.inv(); }
mint operator/(const mint a) const { return mint(*this) /= a; }
bool operator!=(const int a) { return this->x != a; }
bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i)
ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
template <class T> void maxin(T &a, T b) { a = max(a, b); }
template <class T> void minin(T &a, T b) { a = min(a, b); }
template <class M, class N> constexpr common_type_t<M, N> gcd(M a, N b) {
a = abs(a);
b = abs(b);
if (a < b)
return gcd(b, a);
M r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
template <class M, class N> constexpr common_type_t<M, N> lcm(M a, N b) {
return a * b / gcd(a, b);
}
const int N_MAX = 1000005;
ll h, w, k;
int sx, sy, ex, ey;
vector<char> s[N_MAX];
ll bfs() {
vector<vector<ll>> d(h + 1, vector<ll>(w + 1, INF18));
queue<pair<int, int>> que;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
d[sx - 1][sy - 1] = 0;
que.push({sx - 1, sy - 1});
while (que.size()) {
pint now = que.front();
que.pop();
if (now.first == ex - 1 && now.second == ey - 1)
break;
rep(i, 4) {
rep1(j, k) {
int nx = now.first + dx[i] * j, ny = now.second + dy[i] * j;
if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != '@') {
if (d[nx][ny] == INF18) {
d[nx][ny] = d[now.first][now.second] + 1;
que.push({nx, ny});
} else if (d[nx][ny] <= d[now.first][now.second])
break;
} else
break;
}
}
}
return d[ex - 1][ey - 1];
}
int main() {
int x, y;
// 入力
cin >> h >> w >> k;
cin >> sx >> sy >> ex >> ey;
char a;
rep(i, h) {
rep(j, w) {
cin >> a;
s[i].push_back(a);
}
}
// 処理
auto ans = bfs();
// 出力
if (ans == INF18)
cout << -1 << endl;
else
cout << ans << endl;
} | replace | 161 | 162 | 161 | 163 | TLE | |
p02644 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
int n, m, k;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
vector<vector<int>> d;
d.resize(n + 1, vector<int>(m + 1, -1));
vector<vector<char>> c;
c.resize(n + 1, vector<char>(m + 1));
int xa, ya, xb, yb;
cin >> xa >> ya >> xb >> yb;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
}
}
queue<pair<int, int>> q;
d[xa][ya] = 0;
q.push(make_pair(xa, ya));
while (!q.empty()) {
pair<int, int> t = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = t.first, ny = t.second;
for (int i = 1; i <= k; i++) {
nx += dx[dir], ny += dy[dir];
if (nx <= 0 || ny <= 0 || nx > n || ny > m)
break;
if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
break;
if (c[nx][ny] == '@')
break;
d[nx][ny] = d[t.first][t.second] + 1;
q.push(make_pair(nx, ny));
}
}
}
cout << d[xb][yb] << endl;
return 0;
} | #pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
int n, m, k;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
vector<vector<int>> d;
d.resize(n + 1, vector<int>(m + 1, -1));
vector<vector<char>> c;
c.resize(n + 1, vector<char>(m + 1));
int xa, ya, xb, yb;
cin >> xa >> ya >> xb >> yb;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
}
}
queue<pair<int, int>> q;
d[xa][ya] = 0;
q.push(make_pair(xa, ya));
while (!q.empty()) {
pair<int, int> t = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = t.first, ny = t.second;
for (int i = 1; i <= k; i++) {
nx += dx[dir], ny += dy[dir];
if (nx <= 0 || ny <= 0 || nx > n || ny > m)
break;
if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
break;
if (c[nx][ny] == '@')
break;
if (d[nx][ny] == -1) {
d[nx][ny] = d[t.first][t.second] + 1;
q.push(make_pair(nx, ny));
}
}
}
}
cout << d[xb][yb] << endl;
return 0;
} | replace | 42 | 44 | 42 | 46 | TLE | |
p02644 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x) \
for (auto elementPrintVec : x) { \
cout << elementPrintVec << " "; \
} \
cout << endl;
using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};
int main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
queue<P> Q;
Q.push(P(x1, y1));
vector<vector<int>> ans(h, vector<int>(w, -1));
ans[x1][y1] = 0;
while (!Q.empty()) {
P p = Q.front();
Q.pop();
int qx = p.first, qy = p.second;
rep(i, 4) {
repo(j, k) {
int nx = qx + lr[i] * j, ny = qy + up[i] * j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
break;
if (ans[nx][ny] != -1)
continue;
ans[nx][ny] = ans[qx][qy] + 1;
Q.push(P(nx, ny));
}
}
}
cout << ans[x2][y2] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x) \
for (auto elementPrintVec : x) { \
cout << elementPrintVec << " "; \
} \
cout << endl;
using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};
int main() {
int h, w, k;
cin >> h >> w >> k;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
vector<string> s(h);
rep(i, h) { cin >> s[i]; }
queue<P> Q;
Q.push(P(x1, y1));
vector<vector<int>> ans(h, vector<int>(w, -1));
ans[x1][y1] = 0;
while (!Q.empty()) {
P p = Q.front();
Q.pop();
int qx = p.first, qy = p.second;
rep(i, 4) {
repo(j, k) {
int nx = qx + lr[i] * j, ny = qy + up[i] * j;
if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
break;
if (ans[nx][ny] != -1 && ans[nx][ny] <= ans[qx][qy])
break;
if (ans[nx][ny] != -1)
continue;
ans[nx][ny] = ans[qx][qy] + 1;
Q.push(P(nx, ny));
}
}
}
cout << ans[x2][y2] << endl;
return 0;
}
| insert | 45 | 45 | 45 | 47 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.