1 条题解

  • 0
    @ 2025-2-14 21:09:17

    C :

    #include<stdio.h>
    
    int W, H;
    int count;
    int vis[30][30];
    char str[30][30];
    
    struct Point{
        int x, y;
    }Start;
    
    int dx[]={-1,0,1,0}, dy[]={0,1,0,-1};
    
    void Scan_Point();//输入函数
    void DFS(struct Point p);//执行函数
    
    int main()
    {
        while(scanf("%d%d",&H,&W)){
            if(W==0 && H==0) break;
            count=1;
            Scan_Point();
            DFS(Start);
            printf("%d\n",count);
        }
        return 0;
    }
    
    void Scan_Point(){
        int i, j;
        getchar();
        for(i=0; i<W; i++){
            for(j=0; j<H; j++){
                scanf("%c",&str[i][j]);
                vis[i][j]=1;
                if(str[i][j]=='@'){
                    Start.x=i;
                    Start.y=j;
                }
            }
            getchar();
        }
    }
    
    void DFS(struct Point p){
        int a, b;
        int i, j;
        struct Point temp;
        for(i=0; i<4; i++){
            a=p.x+dx[i]; b=p.y+dy[i];
            temp.x=a; temp.y=b;
            if(a>=0 && a<W && b>=0 && b<H && str[a][b]=='.' && vis[a][b]){
                count++;
                vis[a][b]=0;
                DFS(temp);
            }
        }
        return ;
    }
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    struct N
    {
    	int x,y;
    }start,head,tail;
    
    char g[20][20];
    int w,h,sum,v[20][20],f[][2]={{-1,0},{0,-1},{1,0},{0,1}};
    
    int bfs()
    {
    	sum=0;
    	memset(v,0,sizeof(v));
    	v[start.x][start.y]=1;
    	queue<struct N> q;
    	q.push(start);
    	while(!q.empty())
    	{
    		sum++;
    		head=q.front();
    		q.pop();
    		for(int i=0;i<4;i++)
    		{
    			tail.x=head.x+f[i][0];
    			tail.y=head.y+f[i][1];
    			if(!v[tail.x][tail.y]&&tail.x>=0&&tail.x<h&&tail.y>=0&&tail.y<w&&g[tail.x][tail.y]=='.')
    			{
    				q.push(tail);
    				v[tail.x][tail.y]=1;
    			}
    		}
    	}
    	return sum;
    }
    
    int main()
    {
    	int i,j;
    	while(cin>>w>>h,w||h)
    	{
    		for(i=0;i<h;i++)
    			for(j=0;j<w;j++)
    			{
    				cin>>g[i][j];
    				if(g[i][j]=='@')
    				{
    					start.x=i;
    					start.y=j;
    				}
    			}
    		cout<<bfs()<<endl;
    	}
    	return 0;
    }
    

    Pascal :

    program red_and_black;
    const dx:array[1..4]of integer=(0,1,0,-1);
          dy:array[1..4]of integer=(1,0,-1,0);
    var s:string;
        i,j,k,n,f,x,y,nx,ny,w,h,head,tail,sum:integer;
        gr:array[0..40,0..40]of integer;
        dui:array[1..1000,1..2]of integer;
    procedure find(x,y:integer);
    begin
      head:=1;
      tail:=1;
      dui[head,1]:=x;
      dui[head,2]:=y;
      while head<=tail do
        begin
          for i:=1 to 4 do
            begin
              nx:=dui[head,1]+dx[i];
              ny:=dui[head,2]+dy[i];
              if (nx>0)and(nx<=h)and(ny>0)and(ny<=w) then
                begin
                  if gr[nx,ny]<>1 then
                    begin
                      gr[nx,ny]:=1;
                      inc(sum);
                      inc(tail);
                      dui[tail,1]:=nx;
                      dui[tail,2]:=ny;
                    end;{then}
                end;{then}
            end;{for-i}
          inc(head);
        end;{while}
    end;{find}
    begin{main}
      readln(w,h);
      while (w<>0)and(h<>0) do
        begin
          for i:=1 to h do
            begin
              readln(s);
              for j:=1 to w do
                begin
                  if s[j]='.' then gr[i,j]:=0;
                  if s[j]='#' then gr[i,j]:=1;
                  if s[j]='@' then
                    begin
                      x:=i;
                      y:=j;
                      gr[i,j]:=1;
                      sum:=1;
                    end;{then}
                end;{for-j}
            end;{for-i}
          find(x,y);
          writeln(sum);
          readln(w,h);
        end;{while}
    end.
    
    

    Java :

    import java.util.Scanner;
    
    
    public class Main {
    	static int m,n;
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		while(in.hasNextInt()){
    			n = in.nextInt();
    			m = in.nextInt();
    			if(n==0&&m==0)
    				break;
    			String[] s = new String[m];
    			char[][] maze = new char[m][n];
    			int[][] idx = new int[m][n];
    			for(int i=0;i<m;i++){
    				s[i] = in.next();
    				maze[i] = s[i].toCharArray();
    			}
    			for(int i=0;i<m;i++){
    				for(int j=0;j<n;j++){
    					if(idx[i][j]==0&&maze[i][j]=='@')
    						dfs(i,j,idx,maze,1);
    				}
    			}
    			int cnt=0;
    			for(int i=0;i<m;i++)
    				for(int j=0;j<n;j++){
    					if(idx[i][j]>0)
    						cnt++;
    				}
    			System.out.println(cnt);
    		}
    	}
    	public static void dfs(int r,int c,int[][]idx,char[][] maze,int id){
    		if(r<0||r>=m||c<0||c>=n)
    			return;
    		if(idx[r][c]>0||maze[r][c]=='#')
    			return;
    		idx[r][c] = id;
    		
    		dfs(r+1,c,idx,maze,id);
    		dfs(r-1,c,idx,maze,id);
    		dfs(r,c+1,idx,maze,id);
    		dfs(r,c-1,idx,maze,id);
    	}
    }
    
    

    Python :

    def space_input(p=''):
        s=raw_input(p).split()
        for i in range(len(s)):
            s[i]=eval(s[i])
        return tuple(s)
    class Mymap:
        def __init__(self,w,h):
            self.x=0
            self.y=0
            self.height=h
            self.width=w
            self.safe=0
            self.map=[[' ' for i in range(w)] for i in range(h)]
        def getmap(self):
            for i in range(self.height):
                line=raw_input()
                for j in range(self.width):
                    self.map[i][j]=line[j]
                    if line[j]=='@':
                        self.x,self.y=i,j
                        self.map[i][j]='.'
        def open(self,x,y):
            if self.map[x][y]=='.':
                self.map[x][y]='#'
                self.safe+=1
                for i,j in [(x-1,y),(x+1,y),(x,y-1),(x,y+1)]:
                        if 0<=i<self.height and 0<=j<self.width:
                            self.open(i,j)
    w,h=space_input()
    while w or h:
        a=Mymap(w,h)
        a.getmap()
        a.open(a.x,a.y)
        print a.safe
        w,h=space_input()
    
    
    • 1

    信息

    ID
    644
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者