|
@@ -0,0 +1,332 @@
|
|
|
+# Лабораторная работа по теме "массивы и циклы"
|
|
|
+
|
|
|
+### Задание №1 "Змейка"
|
|
|
+```
|
|
|
+ {
|
|
|
+ Console.WriteLine("Введите число:");
|
|
|
+ int N = int.Parse(Console.ReadLine());
|
|
|
+ Console.WriteLine("Результат:");
|
|
|
+ int[,] matrix = new int[N, N];
|
|
|
+
|
|
|
+ int num = 1;
|
|
|
+ for (int i = 0; i < N; i++)
|
|
|
+ {
|
|
|
+ if (i % 2 == 0)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < N; j++)
|
|
|
+ {
|
|
|
+ matrix[i, j] = num++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ for (int j = N - 1; j >= 0; j--)
|
|
|
+ {
|
|
|
+ matrix[i, j] = num++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < N; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < N; j++)
|
|
|
+ {
|
|
|
+ Console.Write(matrix[i, j] + " ");
|
|
|
+ }
|
|
|
+ Console.WriteLine();
|
|
|
+ }
|
|
|
+```
|
|
|
+### Результат:
|
|
|
+```
|
|
|
+ Введите число:
|
|
|
+ 4
|
|
|
+ Результат:
|
|
|
+ 1 2 3 4
|
|
|
+ 8 7 6 5
|
|
|
+ 9 10 11 12
|
|
|
+ 16 15 14 13
|
|
|
+```
|
|
|
+
|
|
|
+### Задание №2 "Художник"
|
|
|
+```
|
|
|
+{
|
|
|
+ string[] mh = Console.ReadLine().Split(' ');
|
|
|
+ int w = int.Parse(mh[0]);
|
|
|
+ int h = int.Parse(mh[1]);
|
|
|
+ int n = int.Parse(Console.ReadLine());
|
|
|
+
|
|
|
+ bool[,] canvas = new bool[w, h];
|
|
|
+
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ string[] rect = Console.ReadLine().Split(' ');
|
|
|
+ int x1 = int.Parse(rect[0]);
|
|
|
+ int y1 = int.Parse(rect[1]);
|
|
|
+ int x2 = int.Parse(rect[2]);
|
|
|
+ int y2 = int.Parse(rect[3]);
|
|
|
+
|
|
|
+ for (int x = x1; x < x2; x++)
|
|
|
+ {
|
|
|
+ for (int y = y1; y < y2; y++)
|
|
|
+ {
|
|
|
+ canvas[x, y] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int unpaintedArea = 0;
|
|
|
+ for (int x = 0; x < w; x++)
|
|
|
+ {
|
|
|
+ for (int y = 0; y < h; y++)
|
|
|
+ {
|
|
|
+ if (!canvas[x, y])
|
|
|
+ {
|
|
|
+ unpaintedArea++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Console.WriteLine("Незакрашенная часть холста:");
|
|
|
+ Console.WriteLine(unpaintedArea);
|
|
|
+}
|
|
|
+```
|
|
|
+### Результат:
|
|
|
+```
|
|
|
+2 1
|
|
|
+1
|
|
|
+1 0 2 1
|
|
|
+Незакрашенная часть холста:
|
|
|
+1
|
|
|
+```
|
|
|
+
|
|
|
+### Задание №3 "Миша и негатив"
|
|
|
+```
|
|
|
+ {
|
|
|
+ string[] dimensions = Console.ReadLine().Split();
|
|
|
+ int n = int.Parse(dimensions[0]);
|
|
|
+ int m = int.Parse(dimensions[1]);
|
|
|
+
|
|
|
+ string[] originalImage = new string[n];
|
|
|
+ string[] generatedNegative = new string[n];
|
|
|
+
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ originalImage[i] = Console.ReadLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.ReadLine();
|
|
|
+
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ generatedNegative[i] = Console.ReadLine();
|
|
|
+ }
|
|
|
+
|
|
|
+ int errorCount = 0;
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < m; j++)
|
|
|
+ {
|
|
|
+ char expectedPixel = originalImage[i][j] == 'B' ? 'W' : 'B';
|
|
|
+ if (generatedNegative[i][j] != expectedPixel)
|
|
|
+ {
|
|
|
+ errorCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Console.WriteLine("Количество ошибок");
|
|
|
+ Console.WriteLine(errorCount);
|
|
|
+ }
|
|
|
+```
|
|
|
+### Результат:
|
|
|
+```
|
|
|
+ 2 2
|
|
|
+ WB
|
|
|
+ BW
|
|
|
+
|
|
|
+ WW
|
|
|
+ BB
|
|
|
+ Количество ошибок
|
|
|
+ 2
|
|
|
+```
|
|
|
+
|
|
|
+### Задание №4 "База террорристов"
|
|
|
+```
|
|
|
+{
|
|
|
+ Console.WriteLine("Найдите базу террористов в пустыне.");
|
|
|
+ string[] baseSize = Console.ReadLine().Split();
|
|
|
+ int nb = int.Parse(baseSize[0]);
|
|
|
+ int mb = int.Parse(baseSize[1]);
|
|
|
+
|
|
|
+ char[,] basePlan = new char[nb, mb];
|
|
|
+ for (int i = 0; i < nb; i++)
|
|
|
+ {
|
|
|
+ string line = Console.ReadLine();
|
|
|
+ for (int j = 0; j < mb; j++)
|
|
|
+ {
|
|
|
+ basePlan[i, j] = line[j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ string[] desertSize = Console.ReadLine().Split();
|
|
|
+ int nd = int.Parse(desertSize[0]);
|
|
|
+ int md = int.Parse(desertSize[1]);
|
|
|
+
|
|
|
+ char[,] desertMap = new char[nd, md];
|
|
|
+ for (int i = 0; i < nd; i++)
|
|
|
+ {
|
|
|
+ string line = Console.ReadLine();
|
|
|
+ for (int j = 0; j < md; j++)
|
|
|
+ {
|
|
|
+ desertMap[i, j] = line[j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0; i <= nd - nb; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j <= md - mb; j++)
|
|
|
+ {
|
|
|
+ bool match = true;
|
|
|
+ for (int k = 0; k < nb; k++)
|
|
|
+ {
|
|
|
+ for (int l = 0; l < mb; l++)
|
|
|
+ {
|
|
|
+ if (basePlan[k, l] == '#' && desertMap[i + k, j + l] == '.')
|
|
|
+ {
|
|
|
+ match = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!match) break;
|
|
|
+ }
|
|
|
+ if (match) count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Console.WriteLine("Количество возможных положений базы террористов:");
|
|
|
+ Console.WriteLine(count);
|
|
|
+ }
|
|
|
+```
|
|
|
+### Результат:
|
|
|
+```
|
|
|
+Найдите базу террористов в пустыне.
|
|
|
+3 3
|
|
|
+#..
|
|
|
+#.#
|
|
|
+#..
|
|
|
+5 36
|
|
|
+#......#........................#...
|
|
|
+#.#....#.#...#.........#............
|
|
|
+#......#.#...##....#...##......###..
|
|
|
+.......#.....#...#.#...##........#.#
|
|
|
+...................#.....#.......#..
|
|
|
+Количество возможных положений базы террористов:
|
|
|
+4
|
|
|
+```
|
|
|
+
|
|
|
+### Задание №5 "Морской бой - 2"
|
|
|
+```
|
|
|
+{
|
|
|
+ string[] input = Console.ReadLine().Split(' ');
|
|
|
+ int n = int.Parse(input[0]);
|
|
|
+ int m = int.Parse(input[1]);
|
|
|
+
|
|
|
+ char[,] field = new char[n, m];
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ string row = Console.ReadLine();
|
|
|
+ for (int j = 0; j < m; j++)
|
|
|
+ {
|
|
|
+ field[i, j] = row[j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0; i < n; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < m; j++)
|
|
|
+ {
|
|
|
+ if (field[i, j] == '.')
|
|
|
+ {
|
|
|
+ bool canPlace = true;
|
|
|
+ if (i > 0 && field[i - 1, j] == '*') canPlace = false;
|
|
|
+ if (i < n - 1 && field[i + 1, j] == '*') canPlace = false;
|
|
|
+ if (j > 0 && field[i, j - 1] == '*') canPlace = false;
|
|
|
+ if (j < m - 1 && field[i, j + 1] == '*') canPlace = false;
|
|
|
+
|
|
|
+ if (canPlace) count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine(count);
|
|
|
+ }
|
|
|
+```
|
|
|
+### Результат:
|
|
|
+```
|
|
|
+4 3
|
|
|
+***
|
|
|
+...
|
|
|
+...
|
|
|
+***
|
|
|
+0
|
|
|
+```
|
|
|
+
|
|
|
+### Задание №6 "Проверка на сипатичность"
|
|
|
+```
|
|
|
+{
|
|
|
+ int t = int.Parse(Console.ReadLine());
|
|
|
+ for (int i = 0; i < t; ++i)
|
|
|
+ {
|
|
|
+ Solve();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static void Solve()
|
|
|
+ {
|
|
|
+ string[] nm = Console.ReadLine().Split();
|
|
|
+ int n = int.Parse(nm[0]);
|
|
|
+ int m = int.Parse(nm[1]);
|
|
|
+
|
|
|
+ int[,] grid = new int[n, m];
|
|
|
+ for (int i = 0; i < n; ++i)
|
|
|
+ {
|
|
|
+ string[] row = Console.ReadLine().Split();
|
|
|
+ for (int j = 0; j < m; ++j)
|
|
|
+ {
|
|
|
+ grid[i, j] = int.Parse(row[j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bool pretty = true;
|
|
|
+ for (int i = 0; i < n - 1; ++i)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < m - 1; ++j)
|
|
|
+ {
|
|
|
+ if ((grid[i, j] == grid[i + 1, j] && grid[i, j] == grid[i, j + 1] && grid[i, j] == grid[i + 1, j + 1]))
|
|
|
+ {
|
|
|
+ pretty = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!pretty) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine(pretty ? "YES" : "NO");
|
|
|
+ }
|
|
|
+ ```
|
|
|
+ ### Результат:
|
|
|
+```
|
|
|
+3
|
|
|
+1 1
|
|
|
+0
|
|
|
+YES
|
|
|
+4 4
|
|
|
+1 0 1 0
|
|
|
+1 1 1 0
|
|
|
+0 1 0 1
|
|
|
+0 0 0 0
|
|
|
+YES
|
|
|
+3 3
|
|
|
+0 0 1
|
|
|
+0 0 1
|
|
|
+1 1 1
|
|
|
+NO
|
|
|
+```
|