Has moved to Zhejiang University
Topic http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2
Zhejiang University ACM 1002
#include <iostream>#include<cstdlib>#include<cmath>#include<list>#defineOPEN 1#defineCASTLE 2#defineBLOCK 3#defineWALL 4//Summary: A greedy principle to find the location of a castle that affects the smallest map//param in:map[] Enter the map. n Map size.//param out:min_x,min_y The x, y coordinates of the placed Castle//Return:true found the right place. False there's no place to put the castle .#defineMax_point 10000//Sentinel NumberBOOLFindmininfluenceposition (intmap[][4],intNint&min_x,int&min_y) { intMin_point = Max_point;//Assigning Sentinel Valuesmin_x = Min_y =0; for(inty =0; Y < n; y++) { for(intx =0; x < n; X + +) { if(Map[y][x] = =OPEN) { intCurr_point =1; //Upward for(intj = y-1; J >=0&& map[j][x]! = WALL; j--) { if(Map[j][x] = =OPEN) Curr_point++; } //downward for(intj = y +1; J < n && map[j][x]! = WALL; J + +) { if(Map[j][x] = =OPEN) Curr_point++; } //left for(intj = x-1; J >=0&& map[y][j]! = WALL; j--) { if(Map[y][j] = =OPEN) Curr_point++; } //Right for(intj = x +1; J < n && map[y][j]! = WALL; J + +) { if(Map[y][j] = =OPEN) Curr_point++; } if(Curr_point <min_point) {Min_point=Curr_point; Min_x=x; Min_y=y; } } } } //detect if a castle has been placed if(Min_point! =max_point)return true; Else return false;}//Summary: Place the castle according to location, mark it on the map//param in:map[] input map. n map size. x, y location of the castle placedvoidPlacecastle (intmap[][4],intNintXinty) {Map[y][x]=CASTLE; //Upward for(intj = y-1; J >=0&& map[j][x]! = WALL; j--) {Map[j][x]=BLOCK; } //downward for(intj = y +1; J < n && map[j][x]! = WALL; J + +) {Map[j][x]=BLOCK; } //left for(intj = x-1; J >=0&& map[y][j]! = WALL; j--) {Map[y][j]=BLOCK; } //Right for(intj = x +1; J < n && map[y][j]! = WALL; J + +) {Map[y][j]=BLOCK; }}intMain () {intmap[4][4]; Std::list<int>answer; intN; CharC; //reading DataStd::cin>>N; while(n! =0) { //reading map Data for(inty =0; Y < n; y++) { for(intx =0; x < n; X + +) {std::cin>>C; if(c = ='.') Map[y][x]=OPEN; ElseMap[y][x]=WALL; } } //Working with Maps intCastle_number =0; intmin_x, min_y; while(Findmininfluenceposition (map, N, min_x, min_y) = =true) {Castle_number++; Placecastle (map, N, min_x, min_y); } //Record DataAnswer.push_back (Castle_number); //Enter Next NumberStd::cin>>N; } //number of outputs for(std::list<int>::iterator It=answer.begin (); It! = Answer.end (); ++it) std::cout<<*it<<"\ n"; return 0;}
[ACM 1002] Zhejiang University fire Net