QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#55694#1970. Social DistancingAs3b_team_f_masr#AC ✓201ms18332kbC++3.1kb2022-10-14 22:35:002022-10-14 22:35:02

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-14 22:35:02]
  • Judged
  • Verdict: AC
  • Time: 201ms
  • Memory: 18332kb
  • [2022-10-14 22:35:00]
  • Submitted

answer

#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define sc second
const int N=505;
char a[N][N];
pair<int,int> disc[N][N];
int dis[N][N],m,n,v[N][N];
int di[4]={0,1,0,-1};
int dj[4]={1,0,-1,0};
void bfs()
{
    set<pair<pair<int,int>,pair<int,int>>>s;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]=='*')
            {
                s.insert({{0,0},{i,j}});
                disc[i][j]={0,0};

            }
        }
    }
    while(!s.empty())
    {
        pair<int,int> p=s.begin()->sc;
        s.erase(s.begin());
        for(int i=0;i<4;i++)
        {
            if(p.fi+di[i]<m&&p.fi+di[i]>=0&&p.sc+dj[i]>=0&&p.sc+dj[i]<n)
            {
                int x=p.fi+di[i];
                int y=p.sc+dj[i];
                if(max(disc[x][y].fi,disc[x][y].sc)>max(disc[p.fi][p.sc].fi+abs(di[i]),disc[p.fi][p.sc].sc+abs(dj[i])))
                {
                    v[x][y]=1;
                    s.erase({{disc[x][y].fi,disc[x][y].sc},{x,y}});
                    disc[x][y]={disc[p.fi][p.sc].fi+abs(di[i]),disc[p.fi][p.sc].sc+abs(dj[i])};
                    s.insert({{disc[x][y].fi,disc[x][y].sc},{x,y}});
                }
            }
        }
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            dis[i][j]=max(disc[i][j].fi,disc[i][j].sc);
        }
    }

}
int dp[N][N];
pair<int,int> st,en;
int vis[N][N];
void solve()
{
    set<pair<int,pair<int,int>>>s;
    s.insert({-dis[st.fi][st.sc],{st.fi,st.sc}});
    dp[st.fi][st.sc]=dis[st.fi][st.sc];
    while(!s.empty())
    {
        pair<int,int> p=s.begin()->sc;
        s.erase(s.begin());
        for(int i=0;i<4;i++)
        {
            if(p.fi+di[i]<m&&p.fi+di[i]>=0&&p.sc+dj[i]>=0&&p.sc+dj[i]<n)
            {
                int x=p.fi+di[i];
                int y=p.sc+dj[i];
                if(a[x][y]!='*'&&a[x][y]!='#'&&(!vis[x][y]||dp[x][y]<min(dp[p.fi][p.sc],dis[x][y])))
                {
                    vis[x][y]=1;
                    s.erase({-dp[x][y],{x,y}});
                    dp[x][y]=min(dp[p.fi][p.sc],dis[x][y]);
                    s.insert({-dp[x][y],{x,y}});
                }
            }
        }
    }
}
int main()
{
	cin>>m>>n;
	int x=0;
	en={-1,-1};
	st={-1,-1};
	for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>a[i][j];
            dis[i][j]=1000000;
            disc[i][j]={1000000,100000};
            if(a[i][j]=='*')x++;
            if(a[i][j]=='S')st={i,j};
            if(a[i][j]=='E')en={i,j};
        }
    }
    bfs();
    pair<int,int>p={-1,-1};
    if(en==p)
    {
        cout<<dis[st.fi][st.sc];
        return 0;
    }
    if(st==p)
    {
        st=en;
        cout<<dis[st.fi][st.sc];
        return 0;
    }

    solve();

    if(vis[en.fi][en.sc])
    {
        if(x)cout<<dp[en.fi][en.sc];
        else cout<<"safe";
    }
    else cout<<-1;
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3648kb

input:

4 5
.*#..
.*..E
..##.
S....

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3548kb

input:

6 8
.......E
........
........
.....**.
........
S.......

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3656kb

input:

3 3
#E#
###
#S#

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3644kb

input:

3 3
.S.
***
.E.

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

3 3
S..
...
..E

output:

safe

result:

ok single line: 'safe'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

2 2
S.
.E

output:

safe

result:

ok single line: 'safe'

Test #7:

score: 0
Accepted
time: 2ms
memory: 4340kb

input:

50 50
.............##.........*....#...##....#..........
.##..........#...#............##.....#....#.....#.
.........##...#....#.......#......#...............
#..##..#.........*...#..........*..............#..
....................*..#.....#.......#........#...
...#...............#................##....

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 159ms
memory: 13760kb

input:

495 496
S......................................................................................................................................................................................................................................................................................................

output:

13

result:

ok single line: '13'

Test #9:

score: 0
Accepted
time: 89ms
memory: 15584kb

input:

500 500
#.*#.##.**.#...#.*.*#.#.*.*#.#.*.##*.#.*..#..*...*##.#.##.#...**#*#.#*...#.#.......#*#...#...*#.#.*#.##..##...*.#.##.*#*..#...*##..##**...*#..##...##...#....*#..##*##....#.#**.#...##..#.##..#*.*#.*.....*#...##...###.##..#**....#####..*..#...#...*.....##*#*##.#..#*.*.*.*.#..*.#*#*#**.......#....

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 61ms
memory: 8748kb

input:

500 500
....................................................................................................................................................................................S..................................................................................................................

output:

-1

result:

ok single line: '-1'

Test #11:

score: 0
Accepted
time: 154ms
memory: 13616kb

input:

500 500
*......................................................................................................................................................................................................................................................................................................

output:

8

result:

ok single line: '8'

Test #12:

score: 0
Accepted
time: 15ms
memory: 8820kb

input:

500 500
S###################################################################################################################################################################################################################################################################################################...

output:

safe

result:

ok single line: 'safe'

Test #13:

score: 0
Accepted
time: 201ms
memory: 18332kb

input:

499 499
.#.....#...#.....#.............##..............#....#.#...#..#......#......#.........................#.#.....#.....#....#..#.#........#....*...#......#..........................#...#......#..............#...*........#.......#...............................................#......................

output:

5

result:

ok single line: '5'

Test #14:

score: 0
Accepted
time: 155ms
memory: 13208kb

input:

500 500
S......................................................................................................................................................................................................................................................................................................

output:

38

result:

ok single line: '38'

Test #15:

score: 0
Accepted
time: 154ms
memory: 13172kb

input:

498 493
*......................................................................................................................................................................................................................................................................................................

output:

5

result:

ok single line: '5'