UVA - 705 Slash Maze(dfs+floodfiil)

本文介绍了一种使用斜杠和反斜杠填充矩形生成迷宫的方法,并通过深度优先搜索算法计算迷宫中的回路数量及最大回路长度。

 Slash Maze 

By filling a rectangle with slashes (/) and backslashes ($\backslash$), youcan generate nice little mazes. Here is an example:

As you can see, paths in the maze cannot branch, so the whole maze onlycontains cyclic paths and paths entering somewhere and leavingsomewhere else. We are only interested in the cycles. In our example,there are two of them.

Your task is to write a program that counts the cycles and finds thelength of the longest one. The length is defined as the number ofsmall squares the cycle consists of (the ones bordered by gray linesin the picture). In this example, the long cycle has length 16 andthe short one length 4.

Input 

The input contains several maze descriptions. Each description begins with oneline containing two integersw andh ($1 \le w, h \le 75$), the width and the height of the maze. The nexth lines represent the maze itself, and containw characters each; all these characters will be either ``/" or ``\".

The input is terminated by a test case beginning with w = h = 0. This case should not be processed.

Output 

For each maze, first output the line ``Maze #n:'', wheren is the number of the maze. Then, output the line``kCycles; the longest has lengthl.'', wherek is the number of cycles in the maze andl the length of the longest of the cycles. If the maze does not contain any cycles, output the line ``There are no cycles.".

Output a blank line after each test case.

Sample Input 

6 4
\//\\/
\///\/
//\\/\
\/\///
3 3
///
\//
\\\
0 0

Sample Output 

Maze #1:
2 Cycles; the longest has length 16.

Maze #2:
There are no cycles.


题意:

要求你算出有多少个回路,并求回路的最大长度是多少(即该回路的面积)。


解析:

可以先把图像放大3倍,并将斜杠标记为1,这样只要遍历4个方向就好了。

如果在dfs时,遇到边界,就将flag标记为false。

最后将所求的结果/3,就是最大的长度。


#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAX = 100;

const int dr[] = {-1 , 0 , 1 , 0};
const int dc[] = { 0 , 1 , 0 , -1};

char grid[MAX][MAX];
int map[3*MAX][3*MAX];
int vis[3*MAX][3*MAX];

int w,h;
bool flag; //flag 用于标记是否构成回路
int step; //step 用于统计步数

void dfs(int r,int c) {
	if( r < 0 || r >= 3*h || c < 0 || c >= 3*w) {
		return ;
	}

	if(map[r][c] == 0 && vis[r][c] == 0) {
		step++;
		vis[r][c] = 1;
		if(r == 0 || r == (3*h -1) || c == 0 || c == (3*w - 1)) {
			flag = false;
		}
		for(int i = 0; i < 4; i++) {
			dfs(r + dr[i],c + dc[i]);
		}
	}
}

int main() {
	int r,c;
	int cas = 1;
	int cnt,max;
	while(scanf("%d%d",&w,&h) != EOF && (w || h)) {
		//输入
		for(int i = 0; i < h; i++) {
			scanf("%s",grid[i]);
		}
		//将迷宫*3
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				r = 3 * i;
				c = 3 * j;
				if(grid[i][j] == '/') {
					map[r][c+2] = map[r+1][c+1] = map[r+2][c] = 1;
				}
				else if(grid[i][j] == '\\'){
					map[r][c] = map[r+1][c+1] = map[r+2][c+2] = 1;
				}
			}
		}
		max = -1000;
		cnt = 0;
		for(int i = 0; i < 3*h; i++) {
			for(int j = 0; j < 3*w; j++) {
				if (vis[i][j] || map[i][j])
					continue;
				flag = true;
				step = 0;
				dfs(i,j);
				if( flag ){
					cnt++;
					if(max < step) {
						max = step;
					}
				}
			}
		}
		printf("Maze #%d:\n",cas++);
		if( cnt > 0 ) {
			printf("%d Cycles; the longest has length %d.\n",cnt,max/3);
		}
		else {
			printf("There are no cycles.\n");
		}
		printf("\n");
	}
	return 0;
}


func TransferDirectory(client *ssh.Client, localDir, remoteDir string) error { fmt.Println("++++++++++--------------------++++++++++") fmt.Println(localDir) fmt.Println(remoteDir) fmt.Println("++++++++++--------------------++++++++++") sftpClient, err := sftp.NewClient(client) if err != nil { return err } defer sftpClient.Close() // 确保远程目录存在并设置权限 if err := sftpClient.MkdirAll(remoteDir); err != nil { return fmt.Errorf("创建远程目录失败: %v", err) } // 设置远程目录权限为755 chmodCmd := fmt.Sprintf("chmod 755 %s", remoteDir) session, err := client.NewSession() if err != nil { return fmt.Errorf("无法创建SSH会话: %v", err) } defer session.Close() if err := session.Run(chmodCmd); err != nil { return fmt.Errorf("设置目录权限失败: %v", err) } err = filepath.Walk(localDir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } // 计算相对路径 relPath, err := filepath.Rel(localDir, path) if err != nil { return err } // 使用filepath.ToSlash确保路径分隔符统一 relPath = filepath.ToSlash(relPath) remotePath := filepath.Join(remoteDir, relPath) if info.IsDir() { // 创建远程目录 if err := sftpClient.MkdirAll(remotePath); err != nil { return fmt.Errorf("创建远程子目录失败: %v", err) } // 设置远程子目录权限为755 chmodSubDirCmd := fmt.Sprintf("chmod 755 %s", remotePath) if err := session.Run(chmodSubDirCmd); err != nil { return fmt.Errorf("设置子目录权限失败: %v", err) } } else { // 确保文件所在目录存在 remoteFileDir := filepath.Dir(remotePath) if err := sftpClient.MkdirAll(remoteFileDir); err != nil { return fmt.Errorf("创建文件目录失败: %v", err) } // 打开本地文件 localFile, err := os.Open(path) if err != nil { return fmt.Errorf("无法打开本地文件: %v", err) } defer localFile.Close() // 创建远程文件 remoteFile, err := sftpClient.Create(remotePath) if err != nil { return fmt.Errorf("创建远程文件失败: %v", err) } defer remoteFile.Close() // 复制文件内容 if _, err := io.Copy(remoteFile, localFile); err != nil { return fmt.Errorf("文件内容复制失败: %v", err) } } return nil }) return err }在我原有的代码上改!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!别乱改函数
最新发布
11-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值