Advertisement
Virtual_Universe

spike c#

May 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. namespace spike
  4. {
  5.     class MainClass
  6.     {
  7.         public static void Main (string[] args)
  8.         {
  9.             StreamReader inf = new StreamReader ("spike.in");
  10.             StreamWriter ouf = new StreamWriter ("spike.out");
  11.             ///////////////////////////////////////////////
  12.             String[] nmk = inf.ReadLine ().Split (' ');
  13.             int n = Convert.ToInt32(nmk[0]);
  14.             int m = Convert.ToInt32(nmk[1]);
  15.             int k = Convert.ToInt32(nmk[2]);
  16.             ///////////////////////////////////////////////
  17.             int[,] city = new int[n,m];
  18.             bool[,] pegas = new bool[n, m];
  19.             int[,] weight = new int[n,m];
  20.  
  21.             for(int i = 0;i<n;i++)
  22.                 for(int j = 0;j<m;j++)
  23.                     weight[i,j] = 10000000;
  24.  
  25.             for (int i = 0; i < n; i++) {
  26.                 string[] city_str = inf.ReadLine().Split (' ');
  27.                 for (int j = 0; j < m; j++)
  28.                     city [i, j] = Convert.ToInt32(city_str [j]);
  29.             }
  30.             for (int i = 0; i < k; i++) {
  31.                 string[] pegas_xy = inf.ReadLine().Split (' ');
  32.                 pegas [Convert.ToInt32(pegas_xy [0]) - 1, Convert.ToInt32(pegas_xy [1])-1] = true;
  33.             }
  34.             ////////////////////////////////////////////////
  35.             weight[0,0] = 0;
  36.             for (int y = 0; y < n; y++) {
  37.                 for (int x = 0; x < m; x++) {
  38.                     if (0 <= x + 1 && x + 1 < m && weight [y, x + 1] > weight [y, x] + city [y, x])
  39.                         weight [y, x + 1] = weight [y, x] + city [y, x];
  40.                     if (0 <= y + 1 && y + 1 < n && weight [y + 1, x] > weight [y, x] + city [y, x])
  41.                         weight [y + 1, x] = weight [y, x] + city [y, x];
  42.                     if (pegas [y, x]) {
  43.                         if (0 <= x + 1 && x + 1 < m && 0 <= y + 2 && y + 2 < n && weight [y + 2, x + 1] > weight [y, x] + city [y, x])
  44.                             weight [y + 2, x + 1] = weight [y, x] + city [y, x];
  45.                         if (0 <= x + 2 && x + 2 < m && 0 <= y + 1 && y + 1 < n && weight [y + 1, x + 2] > weight [y, x] + city [y, x])
  46.                             weight [y + 1, x + 2] = weight [y, x] + city [y, x];
  47.                     }
  48.                 }
  49.             }
  50.             ouf.WriteLine(weight[n - 1,m - 1]+city[n - 1,m - 1]);
  51.             ouf.Close ();
  52.             inf.Close();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement