QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125407#6739. TeleportawellwellTL 1ms104856kbC++142.9kb2023-07-16 17:34:312023-07-16 17:34:35

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 17:34:35]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:104856kb
  • [2023-07-16 17:34:31]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define forn(i,p,q) for(long long i = p;i <=q;i++)
long long read()
{
 	long long t;
 	cin >> t;
  	return t;
}
#define all(p) p.begin(),p.end()
const int N = 5005;
#define pii pair<int,int>
#define x first
#define y second
set <int> st[2 * N];
int dis[N][N];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int n, k;
bool ps[N][N];
int jump[2 * N][N];
inline bool check(int i,int j)
{
	return i <= n && j <= n && i >= 1 && j >= 1;
}
inline void ers(int i, int j)
{
	st[i-j+n].erase(i);
}
void BFS()
{
	queue <pii> qu;
	qu.emplace(1,1);
	ers(1,1);
	dis[1][1] = 0;
	while(!qu.empty())
	{
		auto pt = qu.front();
		qu.pop();
        forn(i,0,3)
        {
            pii nw = make_pair(pt.x+dx[i], pt.y + dy[i]);
            if(check(nw.x, nw.y) && ps[nw.x][nw.y] && dis[nw.x][nw.y] == -1)
            {
                dis[nw.x][nw.y] = dis[pt.x][pt.y] + 1;
                qu.push(nw);
            }
        }
        forn(i,1,k/2)
        {
            pii nw = make_pair(pt.x+i, pt.y + i);
            if(!check(nw.x, nw.y)) break;
            if(dis[nw.x][nw.y] != -1 && dis[nw.x][nw.y] < dis[pt.x][pt.y]) break;
            int & jp = jump[nw.x-nw.y+n][nw.x];
            if(jp)
            {
                i = jp - pt.x + 1;
                jp = max(jp, k / 2);
            }
            else
            {
                jump[nw.x-nw.y+n][nw.x] = k / 2;
                if(check(nw.x, nw.y) && ps[nw.x][nw.y] && dis[nw.x][nw.y] == -1)
                {
                    dis[nw.x][nw.y] = dis[pt.x][pt.y] + 1;
                    qu.push(nw);
                }
            }
        }
        forn(i,1,(k+1)/2)
        {
            pii nw = make_pair(pt.y+i, pt.x+i-1);
            if(!check(nw.x, nw.y)) break;
            if(dis[nw.x][nw.y] != -1 && dis[nw.x][nw.y] < dis[pt.x][pt.y]) break;
            int & jp = jump[nw.x-nw.y+n][nw.x];
            if(jp)
            {
                i = jp - pt.y + 1;
                jp = max(jp, (k+1)/2);
            }
            else
            {
                jp = (k+1)/2;
                if(check(nw.x, nw.y) && ps[nw.x][nw.y] && dis[nw.x][nw.y] == -1)
                {
                    dis[nw.x][nw.y] = dis[pt.x][pt.y] + 1;
                    qu.push(nw);
                }
            }
        }
        if(dis[n][n] != -1) return ;
	}
}
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
int main()
{
    scanf("%d %d\n", &n, &k);
	memset(dis,-1,sizeof(dis));
	forn(i,1,n)
    {
        forn(j,1,n)
        {
            ps[i][j] = getchar() == '.';
        }
        getchar();
    }
    for(int i = n;i;i--) for(int j = n;j;j--)
    {
        if(!ps[i][j])
        {
            jump[i-j+n][i] = max(jump[i-j+n][i+1],i);
        }
    }
	BFS();
    printf("%d", dis[n][n]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 104856kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 104616kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Time Limit Exceeded

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:


result: