Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define NMAX 102
- #define KMAX 1001
- void read(FILE *fin, int *N, int *M, int *K, int D[], int A[][NMAX]) {
- int i, j;
- fscanf(fin, "%d %d %d", &*N, &*M, &*K);
- for(i = 1; i <= *N; ++i)
- for(j = 1; j <= *M; ++j) {
- fscanf(fin, "%d ", &A[i][j]);
- if(A[i][j] == 1)
- A[i][j] = 16;
- }
- int ii;
- for(ii = 0; ii < *K; ++ii) {
- fscanf(fin, "%d", &D[ii]);
- D[ii] = 1 << D[ii];
- //modificam putin codificarea directiilor pentru a putea lucra
- // mai bine cu ele
- }
- D[*K] = 15;
- }
- void edging(int N, int M, int A[][NMAX]) {
- int i;
- for(i = 1; i <= M; ++i)
- A[0][i] = A[N + 1][i] = 16;
- for(i = 1; i <= N; ++i)
- A[i][0] = A[i][M + 1] = 16;
- }
- int DFS(int N, int M, int A[][NMAX], int x, int y,
- int D[], int ii, int K, bool F[][NMAX]) {
- static int count = 0;
- if(ii == K) {
- if(F[x][y] == false) {
- ++count;
- F[x][y] = true;
- }
- return;
- }
- int dir = D[ii];
- int next_dir = D[ii + 1];
- int i;
- if(dir == 1)//sus
- for(i = x - 1; ((A[i][y] & dir) || A[i - 1][y] == 16) && i > 0; --i) {
- if(A[i][y] & next_dir)
- DFS(N, M, A, i, y, D, ii + 1, K, F);
- }
- else if(dir == 2)//jos
- for(i = x + 1; ((A[i][y] & dir) || A[i + 1][y] == 16) && i <= N; ++i) {
- if(A[i][y] & next_dir)
- DFS(N, M, A, i, y, D, ii + 1, K, F);
- }
- else if(dir == 4)//stanga
- for(i = y - 1; ((A[x][i] & dir) || A[x][i - 1] == 16) && i > 0; --i) {
- if(A[x][i] & next_dir)
- DFS(N, M, A, x, i, D, ii + 1, K, F);
- }
- else if(dir == 8)//stanga
- for(i = y + 1; ((A[x][i] & dir) || A[x][i + 1] == 16) && i <= M; ++i) {
- if(A[x][i] & next_dir)
- DFS(N, M, A, x, i, D, ii + 1, K, F);
- }
- return count;
- }
- int solve(int N, int M, int K, int A[][NMAX], int D[]) {
- int i, j, start_i, start_j;
- bool F[NMAX][NMAX];
- int ii = 0;
- edging(N, M, A);
- for(i = 1; i <= N; ++i)
- for(j = 1; j <= M; ++j) {
- F[i][j] = false;
- if(A[i][j] == 0) {
- if(A[i - 1][j] < 16) //sus
- A[i][j] |= 1;
- if(A[i + 1][j] < 16) //jos
- A[i][j] |= 2;
- if(A[i][j - 1] < 16) //stanga
- A[i][j] |= 4;
- if(A[i][j + 1] < 16) //dreapta
- A[i][j] |= 8;
- } else if(A[i][j] == 2) {
- A[i][j] = 0;
- if(A[i - 1][j] < 16) //sus
- A[i][j] |= 1;
- if(A[i + 1][j] < 16) //jos
- A[i][j] |= 2;
- if(A[i][j - 1] < 16) //stanga
- A[i][j] |= 4;
- if(A[i][j + 1] < 16) //dreapta
- A[i][j] |= 8;
- start_i = i;
- start_j = j;
- }
- }
- int rez = DFS(N, M, A, start_i, start_j,
- D, ii, K, F);
- return rez;
- }
- int main() {
- int N, M, K, D[KMAX];
- int A[NMAX][NMAX];
- FILE *fin = fopen("labirint.in", "r");
- FILE *fout = fopen("labirint.out", "w");
- //
- read(fin, &N, &M, &K, D, A);
- fprintf(fout, "%d", solve(N, M, K, A, D));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement