SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Text; | |
| 5 | ||
| 6 | namespace Beaver_Work | |
| 7 | {
| |
| 8 | class Program | |
| 9 | {
| |
| 10 | //деклариране на константите | |
| 11 | const char BEAVER = 'B'; | |
| 12 | const char FISH = 'F'; | |
| 13 | const char FREE = '-'; | |
| 14 | const char FIRST_WOOD = 'a'; | |
| 15 | const char LAST_WOOD = 'z'; | |
| 16 | ||
| 17 | static void Main(string[] args) | |
| 18 | {
| |
| 19 | int n = int.Parse(Console.ReadLine()); | |
| 20 | //инициализиране на променливите и запълване на матрицата | |
| 21 | int countWoods = 0; | |
| 22 | ||
| 23 | char[,] pond = new char[n, n]; | |
| 24 | ||
| 25 | int maxRowPond = pond.GetUpperBound(0); | |
| 26 | int minRowPond = pond.GetLowerBound(0); | |
| 27 | ||
| 28 | int maxColPond = pond.GetUpperBound(1); | |
| 29 | int minColPond = pond.GetLowerBound(1); | |
| 30 | ||
| 31 | int currentBeaverRow = 0; | |
| 32 | int currentBeaverCol = 0; | |
| 33 | ||
| 34 | for (int r = 0; r < n; r++) | |
| 35 | {
| |
| 36 | char[] temp = Console.ReadLine().Split(' ').Select(char.Parse).ToArray();
| |
| 37 | ||
| 38 | for (int c = 0; c < temp.Length; c++) | |
| 39 | {
| |
| 40 | if (IsHasWood(temp[c])) | |
| 41 | {
| |
| 42 | countWoods++; | |
| 43 | } | |
| 44 | ||
| 45 | if (temp[c] == BEAVER) | |
| 46 | {
| |
| 47 | currentBeaverRow = r; | |
| 48 | currentBeaverCol = c; | |
| 49 | } | |
| 50 | pond[r, c] = temp[c]; | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | List<char> collectedWool = new List<char>(); | |
| 55 | ||
| 56 | bool isLoopExit = false; | |
| 57 | bool isCollectAllWood = false; | |
| 58 | ||
| 59 | // цикъл за обработка на командите за движение | |
| 60 | while ((!isCollectAllWood) && (!isLoopExit)) | |
| 61 | {
| |
| 62 | string input = Console.ReadLine().ToLower(); | |
| 63 | ||
| 64 | if (input.Equals("end"))
| |
| 65 | {
| |
| 66 | isLoopExit = true; | |
| 67 | } | |
| 68 | else | |
| 69 | {
| |
| 70 | //начални стойности на стъпките | |
| 71 | int rowStep = 0; | |
| 72 | int colStep = 0; | |
| 73 | ||
| 74 | //инициализиране на стойностите на стъпките съобразно подадените команди за посока | |
| 75 | switch (input) | |
| 76 | {
| |
| 77 | case "up": | |
| 78 | rowStep--; | |
| 79 | break; | |
| 80 | case "down": | |
| 81 | rowStep++; | |
| 82 | break; | |
| 83 | case "left": | |
| 84 | colStep--; | |
| 85 | break; | |
| 86 | case "right": | |
| 87 | colStep++; | |
| 88 | break; | |
| 89 | default: | |
| 90 | break; | |
| 91 | } | |
| 92 | //изчисляване на новите координати на бобъра | |
| 93 | int nextRowPosition = currentBeaverRow + rowStep; | |
| 94 | int nextColPosition = currentBeaverCol + colStep; | |
| 95 | ||
| 96 | //проверка за валидност на новите координати на бобъра | |
| 97 | if (IsValidIndex(maxRowPond, maxColPond, nextRowPosition, nextColPosition)) | |
| 98 | {
| |
| 99 | // ако новите координати са валидни маркиране на старата позиция на бобъра като свободна и задаване на | |
| 100 | //нови текущи координати за позиция на бобъра | |
| 101 | pond[currentBeaverRow, currentBeaverCol] = FREE; | |
| 102 | ||
| 103 | currentBeaverRow = nextRowPosition; | |
| 104 | currentBeaverCol = nextColPosition; | |
| 105 | ||
| 106 | char charInBeaverPosition = pond[currentBeaverRow, currentBeaverCol]; | |
| 107 | ||
| 108 | //ако на новата позиция на бобъра има риба | |
| 109 | if (charInBeaverPosition.Equals(FISH)) | |
| 110 | {
| |
| 111 | pond[currentBeaverRow, currentBeaverCol] = FREE; | |
| 112 | ||
| 113 | // придвижване с една позиция напред под вода | |
| 114 | int goUnderWaterRow = currentBeaverRow + rowStep; | |
| 115 | int goUnderWaterCol = currentBeaverCol + colStep; | |
| 116 | ||
| 117 | // ако новата позоция е валидна, преместване на текущите координати на бобъра до края на езерото | |
| 118 | //в същата посока | |
| 119 | if (IsValidIndex(maxRowPond, maxColPond, goUnderWaterRow, goUnderWaterCol)) | |
| 120 | {
| |
| 121 | int newRowIndex = currentBeaverRow; | |
| 122 | int newColIndex = currentBeaverCol; | |
| 123 | ||
| 124 | if (rowStep == -1) | |
| 125 | {
| |
| 126 | newRowIndex = minRowPond; | |
| 127 | } | |
| 128 | else if (rowStep == 1) | |
| 129 | {
| |
| 130 | newRowIndex = maxRowPond; | |
| 131 | } | |
| 132 | else if (colStep == -1) | |
| 133 | {
| |
| 134 | newColIndex = minColPond; | |
| 135 | } | |
| 136 | else | |
| 137 | {
| |
| 138 | newColIndex = maxColPond; | |
| 139 | } | |
| 140 | ||
| 141 | currentBeaverRow = newRowIndex; | |
| 142 | currentBeaverCol = newColIndex; | |
| 143 | } | |
| 144 | else | |
| 145 | {
| |
| 146 | // ако рибата е била на ръба преместване на новите координати на бобъра на отсрещния бряг на езерото | |
| 147 | ||
| 148 | //ако преместването е по дължината на реда (преместване по хоризонтал) | |
| 149 | if (goUnderWaterRow == currentBeaverRow) | |
| 150 | {
| |
| 151 | // преместване на текущите координати на бобъра на срещуположния край на реда | |
| 152 | currentBeaverRow = goUnderWaterRow; | |
| 153 | currentBeaverCol = maxColPond + 1 + (-1) * goUnderWaterCol * colStep; | |
| 154 | } | |
| 155 | // ако преместването е по височината на колоната (движение по вертикал) | |
| 156 | else if (goUnderWaterCol == currentBeaverCol) | |
| 157 | {
| |
| 158 | //преместване на срещуположния край на колоната | |
| 159 | currentBeaverRow = maxRowPond + 1 + (-1) * goUnderWaterRow * rowStep; | |
| 160 | currentBeaverCol = goUnderWaterCol; | |
| 161 | } | |
| 162 | } | |
| 163 | } | |
| 164 | ||
| 165 | // проверка дали на текущата позиция на бобъра има дърво | |
| 166 | if (IsHasWood(charInBeaverPosition)) | |
| 167 | {
| |
| 168 | collectedWool.Add(charInBeaverPosition); | |
| 169 | countWoods--; | |
| 170 | pond[currentBeaverRow, currentBeaverCol] = FREE; | |
| 171 | ||
| 172 | //Ако бобъра е събрал всички дървета установяване на флага за събрани всички дървета | |
| 173 | if (countWoods == 0) | |
| 174 | {
| |
| 175 | isCollectAllWood = true; | |
| 176 | } | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | //ако бобъра се "удари" в ръба на езерото | |
| 181 | else | |
| 182 | {
| |
| 183 | //ако бобъра е събрал поне едно дърво, губи последното събрано дърво | |
| 184 | if (collectedWool.Count > 0) | |
| 185 | {
| |
| 186 | collectedWool.RemoveAt(collectedWool.Count - 1); | |
| 187 | } | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | //маркиране на текущата позиция на бобъра | |
| 193 | pond[currentBeaverRow, currentBeaverCol] = BEAVER; | |
| 194 | ||
| 195 | // отпечатване на резултатите | |
| 196 | StringBuilder sb = new StringBuilder(); | |
| 197 | ||
| 198 | if (isCollectAllWood) | |
| 199 | {
| |
| 200 | sb.AppendLine($"The Beaver successfully collect {collectedWool.Count} wood branches: {string.Join(", ", collectedWool)}.");
| |
| 201 | } | |
| 202 | else | |
| 203 | {
| |
| 204 | sb.AppendLine($"The Beaver failed to collect every wood branch. There are {countWoods} branches left.");
| |
| 205 | ||
| 206 | } | |
| 207 | ||
| 208 | for (int row = 0; row <= maxRowPond; row++) | |
| 209 | {
| |
| 210 | StringBuilder temp = new StringBuilder(); | |
| 211 | ||
| 212 | for (int col = 0; col <= maxColPond; col++) | |
| 213 | {
| |
| 214 | if (col == maxColPond) | |
| 215 | {
| |
| 216 | temp.Append(pond[row, col].ToString()); | |
| 217 | } | |
| 218 | else | |
| 219 | {
| |
| 220 | temp.Append(pond[row, col].ToString() + " "); | |
| 221 | } | |
| 222 | } | |
| 223 | ||
| 224 | sb.AppendLine(temp.ToString()); | |
| 225 | } | |
| 226 | ||
| 227 | Console.WriteLine(sb.ToString().TrimEnd()); | |
| 228 | } | |
| 229 | ||
| 230 | // помощен метод за проверка на валидността на координатите (индексите на матрицата) на позицията на бобъра | |
| 231 | private static bool IsValidIndex(int maxRowPond, int maxColPond, int nextRowPosition, int nextColPosition) | |
| 232 | {
| |
| 233 | bool result = ((nextRowPosition >= 0) && (nextRowPosition <= maxRowPond)); | |
| 234 | result = ((result) && (nextColPosition >= 0) && (nextColPosition <= maxColPond)); | |
| 235 | return result; | |
| 236 | } | |
| 237 | ||
| 238 | //Помощен метод за определяне дали на текущата позиция на бобъра има дърво | |
| 239 | private static bool IsHasWood(char ch) | |
| 240 | {
| |
| 241 | ||
| 242 | if ((ch >= FIRST_WOOD) && (ch <= LAST_WOOD)) | |
| 243 | {
| |
| 244 | return true; | |
| 245 | } | |
| 246 | ||
| 247 | return false; | |
| 248 | } | |
| 249 | } | |
| 250 | } | |
| 251 |