Submission #2995787


Source Code Expand

Copy
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int H = re.i();
        int W = re.i();
        int K = re.i();
        string S = re.s();
        int[] U = new int[K];
        int[] D = new int[K];
        int[] L = new int[K];
        int[] R = new int[K];
        for(int i=0;i<K;i++){
            U[i] = 1000000000;
            D[i] = 1000000000;
            L[i] = 1000000000;
            R[i] = 1000000000;
        }
        for(int i=0;i<K;i++){
            if(S[i] == 'U'){
                U[i] = 0;
            }
            if(S[i] == 'D'){
                D[i] = 0;
            }
            if(S[i] == 'L'){
                L[i] = 0;
            }
            if(S[i] == 'R'){
                R[i] = 0;
            }
        }
        U[K-1] = Math.Min(U[K-1],U[0]+1);
        for(int i=K-2;i>=0;i--){
            U[i] = Math.Min(U[i],U[i+1]+1);
        }
        U[K-1] = Math.Min(U[K-1],U[0]+1);
        for(int i=K-2;i>=0;i--){
            U[i] = Math.Min(U[i],U[i+1]+1);
        }
        D[K-1] = Math.Min(D[K-1],D[0]+1);
        for(int i=K-2;i>=0;i--){
            D[i] = Math.Min(D[i],D[i+1]+1);
        }
        D[K-1] = Math.Min(D[K-1],D[0]+1);
        for(int i=K-2;i>=0;i--){
            D[i] = Math.Min(D[i],D[i+1]+1);
        }
        R[K-1] = Math.Min(R[K-1],R[0]+1);
        for(int i=K-2;i>=0;i--){
            R[i] = Math.Min(R[i],R[i+1]+1);
        }
        R[K-1] = Math.Min(R[K-1],R[0]+1);
        for(int i=K-2;i>=0;i--){
            R[i] = Math.Min(R[i],R[i+1]+1);
        }
        L[K-1] = Math.Min(L[K-1],L[0]+1);
        for(int i=K-2;i>=0;i--){
            L[i] = Math.Min(L[i],L[i+1]+1);
        }
        L[K-1] = Math.Min(L[K-1],L[0]+1);
        for(int i=K-2;i>=0;i--){
            L[i] = Math.Min(L[i],L[i+1]+1);
        }
        bool[] Map = new bool[H*W];
        int Start = -1;
        int Goal = -1;
        for(int i=0;i<H;i++){
            string S0 = re.s();
            for(int j=0;j<W;j++){
                Map[i*H+j] = S0[j] != '#';
                if(S0[j] == 'S'){
                    Start = i*H+j;
                }
                if(S0[j] == 'G'){
                    Goal = i*H+j;
                }
            }
        }
        Heap heap = new Heap(5000000);
        long[] Cost = new long[H*W];
        for(int i=0;i<H*W;i++){
            Cost[i] = 1000000000000000;
        }
        Cost[Start] = 0;
        bool[] finished = new bool[H*W];
        heap.push(new Edge(0,Start));
        while(!heap.Empty()){
            Edge E = heap.pop();
            long co = E.cost;
            int v = E.v;
            if(finished[v]){
                continue;
            }
            finished[v] = true;
            if(v >= H){
                if(Map[v-H] && U[(int)(co % K)] != 1000000000){
                    if(Cost[v-H] > co+U[(int)(co % K)]+1){
                        Cost[v-H] = co+U[(int)(co % K)]+1;
                        heap.push(new Edge(co+U[(int)(co % K)]+1,v-H));
                    }
                }
            }
            if(v+H < H*W){
                if(Map[v+H] && D[(int)(co % K)] != 1000000000){
                    if(Cost[v+H] > co+D[(int)(co % K)]+1){
                        Cost[v+H] = co+D[(int)(co % K)]+1;
                        heap.push(new Edge(co+D[(int)(co % K)]+1,v+H));
                    }
                }
            }
            if(v%H != H-1){
                if(Map[v+1] && R[(int)(co % K)] != 1000000000){
                    if(Cost[v+1] > co+R[(int)(co % K)]+1){
                        Cost[v+1] = co+R[(int)(co % K)]+1;
                        heap.push(new Edge(co+R[(int)(co % K)]+1,v+1));
                    }
                }
            }
            if(v%H != 0){
                if(Map[v-1] && L[(int)(co % K)] != 1000000000){
                    if(Cost[v-1] > co+L[(int)(co % K)]+1){
                        Cost[v-1] = co+L[(int)(co % K)]+1;
                        heap.push(new Edge(co+L[(int)(co % K)]+1,v-1));
                    }
                }
            }
        }
        if(Cost[Goal] == 1000000000000000){
            sb.Append("-1\n");
        }
        else{
            sb.Append(Cost[Goal]+"\n");
        }
    }
}
struct Edge{
    public long cost;
    public int v;
    public Edge(long c,int v0){
        cost = c;
        v = v0;
    }
}
class Heap{
    public int size;
    Edge[] obj;
    public Heap(int maxsize){
        size = 0;
        obj = new Edge[maxsize];
    }
    public void push(Edge a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(obj[p].cost <= a.cost){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public bool Empty(){
        return size == 0;
    }
    public Edge pop(){
        Edge t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && obj[p+1].cost < obj[p].cost){
                p++;
            }
            if(obj[p].cost < obj[size].cost){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}

Submission Info

Submission Time
Task E - 迷路
User leign
Language C# (Mono 4.6.2.0)
Score 0
Code Size 11110 Byte
Status

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0
All 0 / 500 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt 22 ms 11216 KB
02.txt
03.txt 21 ms 11216 KB
04.txt 21 ms 11216 KB
05.txt 21 ms 9168 KB
06.txt 216 ms 27344 KB
07.txt 213 ms 25296 KB
08.txt 215 ms 27344 KB
09.txt 210 ms 25296 KB
10.txt 209 ms 27344 KB
11.txt 207 ms 27344 KB
12.txt 217 ms 25296 KB
13.txt 205 ms 25296 KB
14.txt 210 ms 25296 KB
15.txt 206 ms 25296 KB
16.txt 212 ms 25296 KB
17.txt 206 ms 25296 KB
18.txt 210 ms 25296 KB
19.txt 205 ms 27344 KB
20.txt 209 ms 27344 KB
21.txt 203 ms 27344 KB
22.txt 205 ms 25296 KB
23.txt 204 ms 25296 KB
24.txt 203 ms 25296 KB
25.txt 206 ms 27344 KB
26.txt 204 ms 27344 KB
27.txt 203 ms 25296 KB
28.txt 201 ms 25296 KB
29.txt 203 ms 25296 KB
30.txt 206 ms 25296 KB
31.txt 46 ms 27344 KB
32.txt 46 ms 27344 KB
33.txt 46 ms 25296 KB
34.txt 46 ms 25296 KB
35.txt 46 ms 25296 KB
36.txt 45 ms 27344 KB
37.txt 46 ms 27344 KB
38.txt 46 ms 25296 KB
39.txt 46 ms 27344 KB
40.txt 93 ms 27344 KB
41.txt 21 ms 11216 KB
s1.txt 21 ms 9168 KB
s2.txt 21 ms 9168 KB
s3.txt 21 ms 11216 KB