Original title link: Click!
Roughly test instructions: the white block indicates the location where the fort can be placed-each turret can attack the turret on a straight line up and down (that is, in its upper and lower left and right lines can not be placed on the fort, to avoid the attack), the black block represents the location of the wall-not placed and can block artillery fire; for a maximum 4 of the grid, the largest number of places to put the fort. Simple analysis: Simple DFS (), because the subject map is very small, do not consider too much, enjoy violence. The DFS branches are drawn first, and then the branches are implemented using functions.
AC Code: (with Notes)
1#include <iostream>2#include <string.h>3#include <algorithm>4#include <stdlib.h>5#include <stdio.h>6 using namespacestd;7 Charmp[5][5];8 intN,ans;9 intdir[4][2]={ {0,1},{0,-1},{1,0},{-1,0} };Ten One intLegalintXintY) {//if point x, Y can place the turret, return 1 A - for(intI=0;i<4; i++){ - for(intk=0; k<=3; k++) {//Four directions, 3 steps. the intXx,yy; -xx=x+dir[i][0]*K; -yy=y+dir[i][1]*K; - if(! (xx>=0&&yy>=0&&xx<=n-1&&yy<=n-1)){//Out of bounds, break, search the next position! + Break; - } + if(mp[xx][yy]=='X'){//There's a wall in one direction. A Break; at } - if(mp[xx][yy]=='D'){//there's a fort in one direction, back 0. - return 0; - } - } - } in return 1; - to } + voidDfsintStep//step indicates the number of points that have been completed - { theans=Max (ans,step); * for(intI=0; i<n;i++) {//each search is to traverse through the graph, because the subject data range thief small, do not consider the tag, etc. $ for(intj=0; j<n;j++){Panax Notoginseng if(mp[i][j]=='.'&&legal (I,J))//to find a legal '. ' - { themp[i][j]='D';//Replace with D, simple and clear +DFS (step+1);//DFS iterations Amp[i][j]='.';//Cancellation Token the } + } - } $ $ } - intMain () - { the while(SCANF ("%d", &n), n!=0){ - for(intI=0; i<n;i++) {//Read GraphWuyiscanf"%s", Mp[i]); the } -ans=0; Wu -Dfs0);//Start searching! Aboutprintf"%d\n", ans); $ } - - return 0; -}View Code
"Hdu-1045,fire net-Pure Violence Simple DFS"