5 // Zadany planek: .=nevime, 0=nic, #<>^vo=kusy lodi
6 static char map[N][N+1] = {
22 // Zadane soucty: radky a sloupce
23 static int sums[2][N] = {
24 { 4, 5, 1, 2, 2, 3, 1, 1, 5, 1, 3, 3, 4 },
25 { 2, 4, 1, 6, 2, 2, 4, 1, 1, 3, 3, 1, 5 },
29 static int ships[] = {
30 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1,
33 #define NSHIPS (int)(sizeof(ships) / sizeof(ships[0]) - 1)
36 static char sea[N][N+1];
37 static int pos[NSHIPS][3];
39 static void show(void)
43 for (int j=0; j<N; j++)
44 printf("%d", sums[1][j]);
46 for (int i=0; i<N; i++)
47 printf("%2d %s %s\n", sums[0][i], sea[i], map[i]);
48 for (int i=0; i<NSHIPS; i++)
49 printf("%d [%d] %d,%d,%d\n", i, ships[i], pos[i][0], pos[i][1], pos[i][2]);
51 for (int i=0; i<N; i++)
52 printf("%s\n", sea[i]);
57 // Cteni z more (dir=preklopeni, x=radek, y=sloupec)
58 static int s(int dir, int x, int y)
60 if (x < 0 || x >= N || y < 0 || y >= N)
62 return dir ? sea[y][x] : sea[x][y];
65 // Cteni z mapy (dir=preklopeni, x=radek, y=sloupec)
66 static int m(int dir, int x, int y)
68 if (x < 0 || x >= N || y < 0 || y >= N)
70 return dir ? map[y][x] : map[x][y];
74 static void p(int dir, int x, int y, int ch)
82 // Da se na pozici (dir,x,y) umistit lod delky len?
83 static int check(int dir, int x, int y, int len)
85 // Vodorovne pocitadlo
86 if (sums[dir][x] < len)
90 for (int yy = y; yy < y+len; yy++)
91 if (sums[!dir][yy] < 1)
94 // Misto v mori (lod s okolim)
95 for (int xx = x-1; xx <= x+1; xx++)
96 for (int yy = y-1; yy <= y+len; yy++)
97 if (s(dir, xx, yy) != '.')
100 // Misto na mape (jen lod sama)
103 if (m(dir, x, y) != '.' && m(dir, x, y) != 'o')
108 if (m(dir, x, y) != '.' && m(dir, x, y) != "<^"[dir] ||
109 m(dir, x, y+len-1) != '.' && m(dir, x, y+len-1) != ">v"[dir])
111 for (int yy = y+1; yy < y+len-1; yy++)
112 if (m(dir, x, yy) != '.' && m(dir, x, yy) != '#')
119 // Umisti na pozici (dir,x,y) lod delky len
120 static void put(int dir, int x, int y, int len)
131 for (int yy = y; yy < y+len; yy++)
137 else if (yy == y+len-1)
145 // Smaze z pozice (dir,x,y) lod delky len
146 static void undo(int dir, int x, int y, int len)
150 for (int yy = y; yy < y+len; yy++)
157 // Porovna dve pozice lexikograficky
158 static int posless(int *a, int *b)
160 for (int i=0; i<3; i++)
163 else if (a[i] > b[i])
168 // Zkontroluje, zda je more konsistentni s mapou
169 static int consistent(void)
171 for (int x=0; x<N; x++)
172 for (int y=0; y<N; y++)
180 ok = (s == '|' || s == '=');
197 static void place(int sh)
207 int dirs = (len == 1 ? 1 : 2);
208 for (int dir = 0; dir < dirs; dir++)
209 for (int x = 0; x < N; x++)
210 for (int y = 0; y <= N - len; y++)
212 pos[sh][0] = dir; pos[sh][1] = x; pos[sh][2] = y;
213 if (sh && ships[sh-1] == len && posless(pos[sh], pos[sh-1]))
215 if (check(dir, x, y, len))
219 undo(dir, x, y, len);
226 for (int i=0; i<N; i++)
228 for (int j=0; j<N; j++)