
正文
dfs搜索JAVA代码 dfsnamedir
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
匈牙利算法 java
#includestdio.h
#includestring.h
bool g[201][201];
int n,m,ans;
bool b[201];
int link[201];
bool init()
{
int _x,_y;
memset(g,0,sizeof(g));
memset(link,0,sizeof(link));
ans=0;
if(scanf("%d%d",n,m)==EOF)return false;
for(int i=1;i=n;i++)
{
scanf("%d",_x);
for(int j=0;j_x;j++)
{
scanf("%d",_y);
g[ i ][_y]=true;
}
}
return true;
}
bool find(int a)
{
for(int i=1;i=m;i++)
{
if(g[a][ i ]==1!b[ i ])
{
b[ i ]=true;
if(link[ i ]==0||find(link[ i ]))
{
link[ i ]=a;
return true;
}
}
}
return false;
}
int main()
{
while(init())
{
for(int i=1;i=n;i++)
{
memset(b,0,sizeof(b));
if(find(i))ans++;
}
printf("%d\n",ans);
}
}
Pascal:
Program matching;
Const
max = 1000;
Var
map : array [1..max, 1..max] of boolean; {邻接矩阵}
match: array [1..max] of integer; {记录当前连接方式}
chk : array [1..max] of boolean; {记录是否遍历过,防止死循环}
m, n, i, t1, t2, ans,k: integer;
Function dfs(p: integer): boolean;
var
i, t: integer;
begin
for i:=1 to k do
if map[p, i] and not chk[ i ] then
begin
chk[ i ] := true;
if (match[ i ] = 0) or dfs(match[ i ]) then {没有被连过 或 寻找到增广路}
begin
match[ i ] := p;
exit(true);
end;{if}
end;{for}
exit(false);
end;{function}
begin{main}
readln(n, m); {N 为二分图左侧点数 M为可连接的边总数}
fillchar(map, sizeof(map), 0);
k:=0;
for i:=1 to m do{initalize}
begin
readln(t1, t2);
map[t1, t2] := true;
if kt2 then k:=t2;
end;{for}
fillchar(match, sizeof(match), 0);
ans := 0;
for i:=1 to n do
begin
fillchar(chk, sizeof(chk), 0);
if dfs(i) then inc(ans);
end;
writeln(ans);
for i:=1 to 1000 do
if match[ i ] 0 then
writeln(match[ i ], '--', i);
end.
相关问答
Q1: JAVA中如何查找字符串
问题很简单dfs搜索JAVA代码:
1.首先dfs搜索JAVA代码,dfs搜索JAVA代码你的数据源是数组dfs搜索JAVA代码,那么要想对每一个值作操作,必须遍历,所以就有如下代码dfs搜索JAVA代码:
for(int i=0;iA.length;i++){
...
}
2.在循环当中,数组中的每一个正在遍历的元素,就是:A[i];
3.String是java的一个字符串处理类,他有一个非常好用的方法就是,
public boolean startsWith(String prefix),测试此字符串是否以指定的前缀开始。 所以:A[i].startsWith("C")如果返回true,那么他就是以C开头的。
4.综上所述,实现很简单,完成代码如下:
public class Test{
public static void main(String[] args){
String[] A ={"CSDF","FDS","CFDSA","DFS","FET"};
for(int i=0;iA.length;i++){
if(A[i].startsWith("C")){
System.out.println(A[i]);
}
}
}
}
总结:
临时写的,代码没有经过测试,但敢保证其正确性的几率很大,祝你成功。
Q2: fastdfs java 怎么使用
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class FastdfsUtils {
/** fdfs初始化文件路径 **/
private Resource r = new ClassPathResource("fdfs_client.properties");
/** 日志**/
private final static Logger LOGGER = Logger.getLogger(FastdfsUtils.class);
/**
* 文件上传
* @throws MyException
* @throws IOException
* @throws FileNotFoundException
*/
public MapString, String fileUpLoad(boolean isEmpty, InputStream inputStream, String fileName)
throws Exception {
MapString, String map = new HashMapString, String();
if (isEmpty) {
throw new Exception("");
}
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] file_buff = null;
if (inputStream != null) {
file_buff = new byte[inputStream.available()];
inputStream.read(file_buff);
}
String[] results = storageClient.upload_file(file_buff,
FilenameUtils.getExtension(fileName), null);
if (results == null) {
throw new Exception("");
}
map.put("fileType", FilenameUtils.getExtension(fileName));
map.put("original", fileName);
map.put("url", results[0] + "/" + results[1]);
map.put("state", "SUCCESS");
map.put("groupName", results[0]);
map.put("fileName", results[1]);
return map;
}
/**
* 文件下载
*/
public byte[] fileDownLoad(String groupName, String fileName) throws IOException, MyException {
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] fileBytes = storageClient.download_file(groupName, fileName);
return fileBytes;
}
/**
* 文件删除
*/
public void fileDelete(String groupName, String fileName) throws FileNotFoundException,
IOException, MyException {
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
storageClient.delete_file(groupName, fileName);
}
}
Q3: 帮忙给个迷宫问题的JAVA程序,最好是用VC..粘的也好,只要可以运行就好..
import java.io.*;
import java.util.Stack;
class MazeCell {
int x, y;
MazeCell() {
}
MazeCell(int i, int j) {
x = i; y = j;
}
boolean equals(MazeCell cell) {
return x == cell.x y == cell.y;
}
}
class Maze {
int rows = 0, cols = 0;
char[][] store;
MazeCell currentCell, exitCell = new MazeCell(), entryCell = new MazeCell();
final char exitMarker = 'e', entryMarker = 'm', visited = '.';
final char passage = '0', wall = '1';
Stack mazeStack = new Stack();
Maze() {
int row = 0, col = 0;
Stack mazeRows = new Stack();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
String str;
System.out.println("Enter a rectangular maze using the following "
+ "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n"
+ "Enter one line at at time; end with Ctrl-z (PC) or Ctrl-D (Unix):");
try {
str = buffer.readLine();
while (str != null) {
row++;
cols = str.length();
str = "1" + str + "1"; // put 1s in the borderline cells;
mazeRows.push(str);
if (str.indexOf(exitMarker) != -1) {
exitCell.x = row;
exitCell.y = str.indexOf(exitMarker);
}
if (str.indexOf(entryMarker) != -1) {
entryCell.x = row;
entryCell.y = str.indexOf(entryMarker);
}
str = buffer.readLine();
}
} catch(IOException eof) {
}
rows = row;
store = new char[rows+2][]; // create a 1D array of char arrays;
store[0] = new char[cols+2]; // a borderline row;
for ( ; !mazeRows.isEmpty(); row--)
store[row] = ((String) mazeRows.pop()).toCharArray();
store[rows+1] = new char[cols+2]; // another borderline row;
for (col = 0; col = cols+1; col++) {
store[0][col] = wall; // fill the borderline rows with 1s;
store[rows+1][col] = wall;
}
}
void display(PrintStream out) {
for (int row = 0; row = rows+1; row++)
out.println(store[row]);
out.println();
}
void pushUnvisited(int row, int col) {
if (store[row][col] == passage || store[row][col] == exitMarker)
mazeStack.push(new MazeCell(row,col));
}
void exitMaze(PrintStream out) {
int row = 0, col = 0;
currentCell = entryCell;
out.println();
while (!currentCell.equals(exitCell)) {
row = currentCell.x;
col = currentCell.y;
display(System.out); // print a snapshot;
if (!currentCell.equals(entryCell))
store[row][col] = visited;
pushUnvisited(row-1,col);
pushUnvisited(row+1,col);
pushUnvisited(row,col-1);
pushUnvisited(row,col+1);
if (mazeStack.isEmpty()) {
display(out);
out.println("Failure");
return;
}
else currentCell = (MazeCell) mazeStack.pop();
}
display(out);
out.println("Success");
}
static public void main (String args[]) {
(new Maze()).exitMaze(System.out);
}
}
试一下这个,是不是你想要的……
___________________________________________________________________________________________________________________________
html代码?你是说你要做一个java applet的网页迷宫小游戏??
Q4: 设计算法判断一个无向图G是否为树。
首先题目中有一处应该是错dfs搜索JAVA代码了。
第2到n+1行dfs搜索JAVA代码,应该改为,第2到m+1行
方法dfs搜索JAVA代码:DFS搜索图,图中的边只可能是树边或反向边,一旦发现反向边,则表明存在环。该算法的复杂度为O(V)。
代码dfs搜索JAVA代码:
/*
设计算法判断一个无向图G是否为树。若是,输出“Yes!”;否则输出“No!”。
输入格式dfs搜索JAVA代码:
第1行是空格分隔的两个整数n和m,分别表示无向图的顶点数和边数,n=10000,m=100000。
第2到m+1行,每行两个整数a和b,表示顶点a和b之间有一条边,1 = a,b =n 。
键盘输入,不必检查输入错误,输入确保正确。
输出格式:
屏幕上显示一行,“yes!”或 “no!”.行末有回车。
样例1
样例2
输入:
4 3
1 2
2 3
3 1
输出:
No!
输入:
2 1
1 2
输出:
Yes!
*/
#includestdio.h
#includestdlib.h
#includestring.h
const int N=10000, M=100000;
bool edge[N][N]; // 数组记录两点是否存在边
bool visit[N]; // 标记该节点是否访问过
bool DFS_check(int x, int y=-1)
{
if (visit[x])
return false;
visit[x] = true;
int i;
for (i=0;iN;i++)
if (edge[x][i] i!=y)
if (visit[i])
return false;
else
if (!DFS_check(i, x))
return false;
return true;
}
int main()
{
int n,m;
scanf("%d%d",n,m);
memset(edge,false,sizeof(edge));
int i,x,y;
for (i=0;im;i++)
{
scanf("%d%d",x,y);
edge[x-1][y-1] = true;
edge[y-1][x-1] = true;
}
memset(visit,false,sizeof(visit));
bool result = DFS_check(0);
if (result)
for (i=0;in;i++)
if (!visit[i])
result = false;
if (result)
printf("Yes!\n");
else
printf("No!\n");
system("pause");
return 0;
}
Q5: dfs(0,0,b)是什么意思?
dfs是深度优先搜索的意思。
这一句应该是dfs算法代码中的一句dfs搜索JAVA代码,意思是调用名为dfs的函数,传入三个参数,分别为0,0,b
前两个参数0,0可能是坐标,后面一个参数是根据实际问题有着具体的含义。
比如“水管工游戏”,其中设置int变量front表示进水口方向,然后dfs函数为dfs搜索JAVA代码:
void dfs(int x,int y,int front)
{
//判断是否越界
......
//判断这个管道是否在路径中已经使用过
......
//当前水管是直管的情况
......
//进水口在左边的情况
//进水口在上边的情况
//进水口在右边的情况
//进水口在下边的情况
......
//当前水管是弯管的情况
......
//进水口在左边的情况
//进水口在上边的情况
//进水口在右边的情况
//进水口在下边的情况
......
}
dfs搜索JAVA代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于dfsnamedir、dfs搜索JAVA代码的信息别忘了在本站进行查找喔。








