Red and Black
The main idea: a person in a rectangular house, you can walk the black area, can not walk the red area, from a certain point, he can go to the maximum number of rooms?
Not much to say, Dfs deep search can, water problem
Just be careful not to mistake the rows and columns. I just missed it once. haha
1#include <stdio.h>2#include <stdlib.h>3 #defineMax_n 204 5 Static intDp[max_n][max_n];6 Static intStartX;7 Static intStarty;8 Static intans;9 Ten voidDfs_search (Const int,Const int,Const int,Const int); One A intMainvoid) - { - intW, H, I, J; the while(~SCANF ("%d%d", &w, &H)) - { - if(W = =0&& H = =0) - Break; +Ans =0;//Start at the beginning. - for(i =0; i < H; i++)//Read Map + { A GetChar (); at for(j =0; J < W; J + +) - { -scanf"%c", &dp[i][j]); - if(Dp[i][j] = ='@'){ -StartX = i; Starty =J; - } in } - } to Dfs_search (StartX, Starty, W, H); +printf"%d\n", ans); - } the * return 0; $ }Panax Notoginseng - voidDfs_search (Const intXConst intYConst intWConst intH) the { +Dp[x][y] ='#'; Aans++; the if(X-1>=0&& Dp[x-1][y]! ='#') +Dfs_search (X-1, Y, W, H); - if(x +1< H && Dp[x +1][y]! ='#') $Dfs_search (x +1, Y, W, H); $ if(Y-1>=0&& Dp[x][y-1] !='#') -Dfs_search (x, Y-1, W, H); - if(Y +1< W && Dp[x][y +1] !='#') theDfs_search (x, y +1, W, H); -}
Dfs:red and Black (POJ 1979)