USACO castle

<pre name="code" class="cpp"><pre>USER: Kevin Samuel [kevin_s1]
TASK: castle
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.003 secs, 3688 KB]
   Test 2: TEST OK [0.005 secs, 3688 KB]
   Test 3: TEST OK [0.008 secs, 3688 KB]
   Test 4: TEST OK [0.008 secs, 3688 KB]
   Test 5: TEST OK [0.003 secs, 3688 KB]
   Test 6: TEST OK [0.005 secs, 3688 KB]
   Test 7: TEST OK [0.014 secs, 3688 KB]
   Test 8: TEST OK [0.005 secs, 3688 KB]

All tests OK.
<p>Your program ('castle') produced all correct answers!  This is your
submission #4 for this problem.  <strong>Congratulations!</strong></p>


/*
ID:kevin_s1
PROG:castle
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <queue>
#include <cmath>

using namespace std;

#define MAXM 100

//gobal variable====
int M,N;
int direct[4][2] = {{-1, 0},{1, 0},{0, 1},{0, -1}};
//north, south, east, west
int castle[MAXM][MAXM];
int visited[MAXM][MAXM];
int visited2[MAXM][MAXM];
int visited3[MAXM][MAXM];
int hasWall[MAXM][MAXM][4];
int cnt, maxarea;
int _count;
//==================

//function==========

void Dec(){
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			int wall = castle[i][j];
			if(wall >= 8){
				hasWall[i][j][1] = 1;
				wall = wall - 8;
			}
			if(wall >= 4){
				hasWall[i][j][2] = 1;
				wall = wall - 4;
			}
			if(wall >= 2){
				hasWall[i][j][0] = 1;
				wall = wall - 2;
			}
			if(wall >= 1){
				hasWall[i][j][3] = 1;
				wall = wall - 1;
			}
		}
	}
}

void DFS(int x, int y, int type){
	if(x < 1 || x > N || y < 1 || y > M)
		return;
	if(visited[x][y] != 0)
		return;
	visited[x][y] = type;
	visited2[x][y] = type;
	_count++;
	if(_count > maxarea){
		maxarea = _count;
	}
	for(int d = 0; d < 4; d++){
		if(hasWall[x][y][d] == 0){
			int xx = x + direct[d][0];
			int yy = y + direct[d][1];
			DFS(xx, yy, type);
		}
	}
	return;
}

void DFS1(int x, int y, int type, int color){
	if(x < 1 || x > N || y < 1 || y > M)
		return;
	if(visited[x][y] != type)
		return;
	if(visited3[x][y] == 1)
		return;
	visited[x][y] = color;
	visited3[x][y] = 1;
	for(int d = 0; d < 4; d++){
		if(hasWall[x][y][d] == 0){
			int xx = x + direct[d][0];
			int yy = y + direct[d][1];
			DFS1(xx, yy, type, color);
		}
	}
	return;
}

void print(){
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			cout<<visited[i][j]<<" ";
		}
		cout<<endl;
	}
}

void print2(){
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			cout<<visited2[i][j]<<" ";
		}
		cout<<endl;
	}
}
//==================

int main(){
	freopen("castle.in","r",stdin);
	freopen("castle.out","w",stdout);
	cin>>M>>N;
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			cin>>castle[i][j];
		}
	}
	memset(hasWall, 0, sizeof(hasWall));
	memset(visited, 0, sizeof(visited));
	memset(visited2, 0, sizeof(visited2));
	memset(visited3, 0, sizeof(visited3));
	Dec();
	cnt = 0;
	maxarea = 0;
	int type = 1;
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= M; j++){
			if(visited[i][j] == 0){
				_count = 0;
				DFS(i, j, type);
				cnt++;
				DFS1(i, j, type, _count);
				type++;
			}
		}
	}
	cout<<cnt<<endl;
	cout<<maxarea<<endl;
	//print2();
	//print();
	//enum the wall
	int mmaxarea = 0;
	int tx = 0, ty = 0, dir = 0;
	for(int x = 1; x <= N; x++){
		for(int y = M; y >= 1; y--){
			for(int d = 0; d < 4; d++){
				if(hasWall[x][y][d] == 1){
					int xx = x + direct[d][0];
					int yy = y + direct[d][1];
					if(xx >= 1 && xx <= N && yy >= 1 && yy <= M){
						if((visited[x][y] + visited[xx][yy]) > mmaxarea && (visited2[x][y] != visited2[xx][yy])){
							mmaxarea = visited[x][y] + visited[xx][yy];
							tx = x;
							ty = y;
							dir = d;
						}
						if((visited[x][y] + visited[xx][yy]) == mmaxarea && (visited2[x][y] != visited2[xx][yy])){
							if(y < ty){
								tx = x;
								ty = y;
								dir = d;
							}
							if(y == ty && x > tx){
								tx = x;
								ty = y;
								dir = d;
							}
						}
					}
				}
			}
		}
	}
	cout<<mmaxarea<<endl;
	if(dir == 1){
		tx = tx + 1;
		dir = 0;
	}
	if(dir == 3){
		ty = ty - 1;
		dir = 2;
	}
	cout<<tx<<" "<<ty<<" ";
	if(dir == 0)
		cout<<"N"<<endl;
	if(dir == 2)
		cout<<"E"<<endl;
	return 0;
}

USACO castle,布布扣,bubuko.com

时间: 2024-10-14 16:52:15

USACO castle的相关文章

USACO Section2.1 The Castle 解题报告

castle解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 有N×M的矩阵,边框都是实际存在的“墙”.如下图: 1 2 3 4 5 6 7 ########################

USACO 2.1 The Castle

题目大意:给你一个城堡让你求有多少房间,最大房间有多大,敲掉一堵墙后最大的房间有多大,敲掉那座墙 思路:比较恶心的bfs题,反正就是bfs使劲敲 /*{ ID:a4298442 PROB:castle LANG:C++ } */ #include<iostream> #include<cstdio> #include<fstream> #include<queue> #include<algorithm> #define pii pair<

USACO 2.1.1 The Castle

{ ID:anniel11 PROG:castle LANG:PASCAL } var a:array[0..50,0..50 ,1..4] of boolean; component:array[0..50,0..50] of integer;//which room does it belong to room_size:array[0..2500] of integer; neighbour:array[0..2500,0..2500] of boolean; m,n,i,j,temp,c

usaco The Castle

题目给了一个二维矩阵,矩阵的每个数字代表一个单位的面积,每个数字转换为二进制,这个四位二进制数的每一位,分别代表了自己的东南西北是否有墙. 题目求房间的数目,最大的自然房间的大小,拆掉某一堵墙之后的可能会造成某两个自然房间合并,求合成最大房间的面积,以及拆除的抢的坐标,以及位置 做法是,对单位面积进行染色,可以相互联通的区域就是一个房间. 再设一个数组Size表示染色为i的房间的大小, 然后枚举拆掉每堵墙之后的情况,合成的房间的大小就是自己和自己相邻的单位面积所代表的房间大小(Size中的值)相

【USACO 2.1】The Castle

/* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<cstdio> #include<algorithm> #include<cstring> #define N 55 using namespace std; int n,m; int a[N][N]; int ans,num,cnt; int rans,rm,d; char di

USACO Section 2.1 The Castle

/* ID: lucien23 PROG: castle LANG: C++ */ /************************************************************************/ /* 求图的连通域问题.利用广度扫描 */ /************************************************************************/ #include <iostream> #include <fs

[【USACO】The Castle(dfs+枚举)

思路很好像,卡了我很久的就是当最大房间一样的时候判断输出哪个的条件, = = 简直无情 /* ID: 18906421 LANG: C++ PROG: castle */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 55; int mat[maxn][maxn][2] = {0}; // 0 1 下 右 int n,m,vis[m

USACO The Castle(flood fill)

题目请点我 题解: 这道题真的好蛋疼啊,首先题意不好理解,搞了半天复杂的要死,有那么多要求,还要求那么多东西,做到一半都不想做了...感觉没什么技术含量,还做起来死费劲儿.但是强迫症非得按顺序做题啊,最后还是一点点把它给调出来了,说什么flood fill,其实也就是那么回事,没什么算法上的技巧,就是见招拆招的感觉... 题意搞懂再做题,题意,不谢! 第一步:根据他的规则把房间画出来,遍历一遍把每个节点四面的墙给补上: 第二步:深搜: 目的1:记录深搜的次数,房间数: 目的2:记录深搜的深度,最

[USACO][暴力]The Castle

是语文题(确信) 题意: 给一个数字矩阵代表一个有很多墙壁的屋子,墙壁的包围可以形成一个个房间,输出最大的房间大小.最大房间位置以及房间数目. 思路: 首先M和N容易反,然后就是拆墙找最大房间的时候要先东西找,再南北找.东西中更西的优先,南北的更南的优先.把这里的东西南北映射成i++,i--,j++,j--这样子. 再就是color数组做标号的时候,如果roomNumber从0开始的话,color初始化就要是-1,然后搜索搜到有-1就不找他了.我这里是roomNumber从1开始.同时搜索的时候