zoj 2412 搜索
来源:互联网 发布:网络搬砖是什么意思 编辑:程序博客网 时间:2024/06/01 08:16
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412
我的代码太长了,太丑了,我的优化方法有问题
一下是我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int sizen=1000;
bool Map[sizen][sizen];
int Mat[sizen][sizen];
int m,n;
int d[4][2]={1,0,-1,0,0,1,0,-1};
void dfs(int x,int y)
{
int i;
int mx,my;
for(i=0;i<4;i++)
{
mx=x+d[i][0];
my=y+d[i][1];
if(Map[mx][my])
{
Map[mx][my]=false;
dfs(mx,my);
}
}
}
int main()
{
int cnt;
int i,j;
char c;
while(scanf("%d%d",&m,&n)!=EOF)
{
cnt=0;
if(m<0||n<0)
break;
memset(Map,false,sizeof(Map));
memset(Mat,0,sizeof(Mat));
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>c;
Map[i*2][j*2]=true;
if(c=='A')
{
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
}
if(c=='B')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
}
if(c=='C')
{
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
}
if(c=='D')
{
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
}
if(c=='E')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
}
if(c=='F')
{
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
}
if(c=='G')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
}
if(c=='H')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
}
if(c=='I')
{
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
}
if(c=='J')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
}
if(c=='K')
{
Mat[i*2-1][j*2]++;
if(Mat[i*2-1][j*2]==2)
Map[i*2-1][j*2]=true;
Mat[i*2+1][j*2]++;
if(Mat[i*2+1][j*2]==2)
Map[i*2+1][j*2]=true;
Mat[i*2][j*2+1]++;
if(Mat[i*2][j*2+1]==2)
Map[i*2][j*2+1]=true;
Mat[i*2][j*2-1]++;
if(Mat[i*2][j*2-1]==2)
Map[i*2][j*2-1]=true;
}
}
for(i=1;i<=2*m+1;i++)
for(j=1;j<=2*n+1;j++)
if(Map[i][j])
{
Map[i][j]=false;
dfs(i,j);
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
一下是我认为很好的代码
#include<stdio.h>
#include<string.h>
#define I (i*3)
#define J (j*3)
char str[51][51];
int map[159][159];
int n, m;
int dir[4][2] =
{
{0, 1}, {0, -1}, {-1, 0}, {1, 0}
};
void dfs(int i, int j)
{
map[i][j] = 0;
for(int k = 0; k < 4; k++) {
int x = i + dir[k][0];
int y = j + dir[k][1];
if(x >= 0 && x < 3*n && y >= 0 && y < 3*m && map[x][y]) {
dfs(x, y);
}
}
}
int main()
{
while(scanf("%d %d%*c", &n, &m) != EOF) {
if(n < 0 || m < 0) break;
memset(map, 0, sizeof(map));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%c", &str[i][j]);
if( str[i][j] == 'A' ) {
map[I][J+1] = map[I+1][J] = map[I+1][J+1] = 1;
}
if( str[i][j] == 'B' ) {
map[I+1][J+1] = map[I+1][J+2] = map[I][J+1] = 1;
}
if( str[i][j] == 'C' ) {
map[I+2][J+1] = map[I+1][J] = map[I+1][J+1] = 1;
}
if( str[i][j] == 'D' ) {
map[I+2][J+1] = map[I+1][J+2] = map[I+1][J+1] = 1;
}
if( str[i][j] == 'E' ) {
map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = 1;
}
if( str[i][j] == 'F' ) {
map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = 1;
}
if( str[i][j] == 'G' ) {
map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = map[I][J+1] = 1;
}
if( str[i][j] == 'H' ) {
map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J] = 1;
}
if( str[i][j] == 'I' ) {
map[I+1][J+2] = map[I+1][J] = map[I+1][J+1] = map[I+2][J+1] = 1;
}
if( str[i][j] == 'J' ) {
map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J+2] = 1;
}
if( str[i][j] == 'K' ) {
map[I][J+1] = map[I+2][J+1] = map[I+1][J+1] = map[I+1][J+2] = map[I+1][J] = 1;
}
}
scanf("%*c");
}
int ans = 0;
for(int i = 0; i < 3*n; i++) {
for(int j = 0; j < 3*m; j++) {
if(map[i][j]) {
dfs(i, j);
ans++;
}
}
}
printf("%d\n", ans);
}
return 0;
}
他的宏定义
#define I (i*3)
#define J (j*3)
感觉特牛,我需要学习一下
还有的是他乘以上的思路太吊了
- zoj 2412 搜索
- ACM-ZOJ 2412 DFS 深度优先搜索
- zoj 1101 搜索
- zoj 1101 二分搜索
- ZOJ 2562 数论+搜索
- 【ZOJ】2580【搜索】【suduku】
- [ZOJ 2961] Spinlock [搜索]
- zoj 1002回溯搜索
- ZOJ - 1204 Additive equations(搜索)
- ZOJ-1984 Genetic Code(搜索)
- ZOJ-1003-Crashing Balloon(搜索)
- ZOJ 1002 Fire Net(搜索)
- ZOJ 1101 Gamblers (简单搜索)
- zoj 1101Gamblers排序,搜索
- zoj 1530 记忆化搜索
- ZOJ 1003 Crashing Balloon 搜索
- ZOJ 3644 记忆化搜索
- zoj 3726 Pocket Cube(搜索)
- Android 开发 之 JNI入门 - NDK从入门到精通Android 开发 之 JNI入门 - NDK从入门到精通
- C语言知识点
- UVA - 11997(优先队列)
- 第8周项目1-求简单分段函数的值
- 寻找和为定值的两个数/多个数
- zoj 2412 搜索
- [Spring] Spring MVC中@Autowired注释失效的可能原因
- IOKit
- POJ1284_Primitive Roots【欧拉函数】
- storm第一篇--概念,例子,参数优化
- hdu 5019
- deb打包+软件图标+加到系统菜单+安装卸载实例
- set集合 HashSet: TreeSet实现Comparable接口,覆盖compareTo方法。 类实现Comparator接口,覆盖compare方法。
- HDU 4756 Install Air Conditioning(次小生成树)