コード例 #1
0
ファイル: 200.cpp プロジェクト: Rayleigh0328/OJ
 void flood_fill(int x, int y, int label, vector<vector<char>>& g)
 {
     if (x < 0 || x>=n) return ;
     if (y < 0 || y>=m) return ;
     if (g[x][y]=='0' || flag[x][y] != 0) return ;
     
     flag[x][y] = label;
     flood_fill(x-1,y,label,g);
     flood_fill(x+1,y,label,g);
     flood_fill(x,y+1,label,g);
     flood_fill(x,y-1,label,g);
 }
コード例 #2
0
ファイル: VECTOR_TO_PIXEL.c プロジェクト: RDerber/REU2016
// UNUSED
// Recursive fill starting from the center of the circle
void flood_fill(unsigned char pixelArray[PIXEL_DIMMENSION][PIXEL_DIMMENSION], 
		int x, int y){
	if (pixelArray[x][y] == 0 && is_bounded_filled(x, y, pixelArray)) {
		pixelArray[x][y] = BLACK;
		
		flood_fill(pixelArray, x - 1, y);
		flood_fill(pixelArray, x + 1, y);
		flood_fill(pixelArray, x, y - 1);
		flood_fill(pixelArray, x, y + 1);
	} else if (is_bounded_filled(x, y, pixelArray)) {
		pixelArray[x][y] = BLACK;
		return;
	} else {
		return;
	}
}
コード例 #3
0
ファイル: 200.cpp プロジェクト: Rayleigh0328/OJ
 int numIslands(vector<vector<char>>& g) {
     if (g.size() == 0) return 0;
     if (g[0].size() == 0) return 0;
     
     n = g.size();
     m = g[0].size();
     
     flag = new int* [n];
     for (int i=0;i<n;++i)
         flag[i] = new int [m];
     for (int i=0;i<n;++i)
         for (int j=0;j<m;++j)
             flag[i][j] = 0;
     
     int cnt = 1;
     for (int i=0;i<n;++i)
         for (int j=0;j<m;++j)
             if (g[i][j]=='1' && flag[i][j]==0)
                 flood_fill(i,j,cnt++,g);
     
     for (int i=0;i<n;++i)
         delete [] flag[i];
     delete [] flag;
     
     return cnt -1;
 }
コード例 #4
0
static int flood_fill (int x, int y, int color)
{
    int c = 0;
    
    if (field [x][y].color != color)
        return c;
    
    if (field [x][y].tag)
        return c;

    c = 1;
    field [x][y].tag = 1;
    
    if (x+1 < STONE_COLS)
        c += flood_fill (x+1, y, color);
    if (x)
        c += flood_fill (x-1, y, color);
    if (y+1 < STONE_LINES)
        c += flood_fill (x, y+1, color);
    if (y)
        c += flood_fill (x, y-1, color);
    return c;
}
コード例 #5
0
ファイル: castle.c プロジェクト: cz717/ojsol
void find_room()
{
	int i, j;
	room_num = -1;
	for (i = 0; i < N; i++) {		/* for each module */
		for (j = 0; j < M; j++) {
			if (room[i][j] == nil) {
				room_num++;
				room[i][j] = visited;
				flood_fill(room_num);
				// printf("\n");
			}
		}
	}
}
コード例 #6
0
ファイル: 2581.c プロジェクト: hjhee/HOJ
int main(void) {
#ifndef ONLINE_JUDGE
    freopen("2581.in", "rb", stdin);
#endif
    int w,b,r,c,i,j,bfill,wfill;
    while(scanf("%d", &n)==1&&n) {
        memset(map, -1, sizeof(map));
        memset(bvis, 0, sizeof(bvis));
        memset(wvis, 0, sizeof(wvis));
        scanf("%d%d", &b, &w);
        for(i=0; i<b; ++i) {
            scanf("%d%d", &r, &c);
            map[r-1][c-1]=B;
        }
        for(i=0; i<w; ++i) {
            scanf("%d%d", &r, &c);
            map[r-1][c-1]=W;
        }
        for(i=0; i<n; ++i)
            for(j=0; j<n; ++j)
                if(map[i][j]==W||map[i][j]==B)
                    flood_fill(i, j, map[i][j]);
        for(bfill=wfill=i=0; i<n; ++i)
            for(j=0; j<n; ++j) {
                if(map[i][j]==B-1)
                    ++bfill;
                else if(map[i][j]==W-1)
                    ++wfill;
            }
        /*
        for(i=0; i<n; ++i){
            for(j=0; j<n; ++j)
                printf("%d ", map[i][j]);
            putchar('\n');
        }
        */
        if(bfill==wfill)
            puts("Draw");
        else if(bfill>wfill)
            printf("Black wins by %d\n", bfill-wfill);
        else
            printf("White wins by %d\n", wfill-bfill);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
#endif
    return 0;
}
コード例 #7
0
ファイル: same-gnome.c プロジェクト: cstrahan/aduni
static void
mark_balls (int x, int y)
{
	if (x == old_x && y == old_y)
		return;
	old_x = x;
	old_y = y;

	untag_all ();
	disable_timeout ();
	if (!field [x][y].color)
		return;
	
	tagged_count = flood_fill (x, y, field [x][y].color);
	
	if (tagged_count > 1)
		ball_timeout_id = gtk_timeout_add (100, move_tagged_balls, 0);
}
コード例 #8
0
static void mark_balls (HWND hwnd, int x, int y)
{
    if (x == old_x && y == old_y)
        return;

    old_x = x;
    old_y = y;

    untag_all (hwnd);
    disable_timeout (hwnd);
    if (!field [x][y].color)
        return;
    
    tagged_count = flood_fill (x, y, field [x][y].color);
    
    if (tagged_count > 1) {
        SetTimer (hwnd, ID_TIMER, 10); 
        ball_timeout_id = ID_TIMER;
    }
}
コード例 #9
0
ファイル: castle.c プロジェクト: Cybuster/Contest-Archive
int main(void){
	openFiles();

	int i, j;

	scanf("%d %d", &width, &height);

	for (j = 0; j < height; j++)
		for (i = 0; i < width; i++)
			scanf("%d", &map[i][j]);

	for (j = 0; j < height; j++){
		for (i = 0; i < width; i++){
			if (!room[i][j]){
				room[i][j] = TOPROCESS;
				flood_fill();
			}
		}
	}

	for (i = 0; i < width; i++){
		for (j = height - 1; j >= 0; j--){
			if (isBlocked(north, map[i][j])){
				tryJoining(i,j,i,j-1,'N');
			}
			if (isBlocked(east, map[i][j])){
				tryJoining(i,j,i+1,j,'E');
			}
		}
	}

	printf("%d\n", nextRoom-1);
	printf("%d\n", largestRoom);
	printf("%d\n", max);
	printf("%d %d %c\n", besty + 1, bestx + 1, bestDir);


	return 0;
}
コード例 #10
0
ファイル: 2581.c プロジェクト: hjhee/HOJ
void flood_fill(int x, int y, int v) {
    int i,tx,ty;
    const int dx[]= {0,0,1,-1};
    const int dy[]= {1,-1,0,0};
    if(v==B)
        bvis[x][y]=1;
    else
        wvis[x][y]=1;
    for(i=0; i<4; ++i) {
        tx=x+dx[i];
        ty=y+dy[i];
        if(0<=tx&&tx<n&&0<=ty&&ty<n&&((v==W&&!wvis[tx][ty])||
                                      (v==B&&!bvis[tx][ty]))&&
                map[tx][ty]!=B&&map[tx][ty]!=W) {
            if(map[tx][ty]==-1)
                map[tx][ty]+=v;
            else if(v==B&&map[tx][ty]==W-1)
                map[tx][ty]+=v;
            else if(v==W&&map[tx][ty]==B-1)
                map[tx][ty]+=v;
            flood_fill(tx, ty, v);
        }
    }
}
コード例 #11
0
ファイル: hw1.cpp プロジェクト: yuanb10/csci1200
int main(int argc, char* argv[]) {
    // check if the argument number is valid
    if (argc < 4) {
        std::cerr << "Usage: "
                  << argv[0]
                  << " input-file output-file [operations] [parameters]"
                  << std::endl;
        return 1;
    }
    // check if the input file is valid
    std::ifstream input(argv[1]);
    if (!input) {
        std::cerr << "Can not open the grades in file " << argv[1] << std::endl;
        return 1;
    }
    // read in the image
    std::vector<std::string> image;
    read_in_image(input, image);

    if (image.size() == 0) {
        std::cout << "Cannot import the image "
                  << argv[1]
                  << ". Try again please. "
                  << std::endl;
        return 1;
    }

    std::ofstream output(argv[2]);   // the output file
    std::string op = argv[3];   // the operation name

    if (op.compare("replace") == 0 && argc == 6) {  // replace
        char before  = argv[4][0];
        char after  = argv[5][0];
        replace(image, before, after);
    } else if (op.compare("dilation") == 0 &&
               (argc == 5 || argc == 6)) {   // dilation operation
        char color = argv[4][0];
        if (argc == 5) {
            // When plus-signed structing element
            dilation(image, color, false);
        } else if (argc == 6 && std::string(argv[5]).compare("square") == 0) {
            // when square-signed structing element
            dilation(image, color, true);
        } else {
            std::cerr << "Usage: "
                      << argv[0]
                      << " input-file output-file dilation color [square]"
                      << std::endl;
            return 1;
        }
    } else if (op.compare("erosion") == 0 &&
               (argc == 6 || argc == 7)) {   // erosion operation
        char color = argv[4][0];
        char pixel = argv[5][0];
        if (argc == 6) {
            // When plus-signed structing element
            erosion(image, color, pixel, false);
        } else if (argc == 7 && std::string(argv[6]).compare("square") == 0) {
            // when square-signed structing element
            erosion(image, color, pixel, true);
        } else {
            std::cerr << "Usage: " << argv[0]
                      << " input-file output-file erosion pixel [square]"
                      << std::endl;
            return 1;
        }
    } else if (op.compare("floodfill") == 0 && argc == 7) {
        // floodfill operation
        int row = atoi(argv[4]);
        int col = atoi(argv[5]);
        if (!isValid(image, row, col)) {
            std::cerr << "Error: invalid arguments "
                      << row << " and " << col
                      << std::endl;
            return 1;
        }
        char filler = argv[6][0];
        flood_fill(image, row, col, filler);
    } else {   // invalid input command
        std::cerr << "Invalid command." << std::endl;
        std::cerr << "Usage: " << argv[0]
                  << " input-file output-file [operations] [parameters]"
                  << std::endl;
        return 1;
    }

    // write the modified image to a file
    if (output.is_open()) {
        output_image(image, output);
        output.close();
    } else {
        std::cerr << "Cannot output to " << argv[3] << std::endl;
        return 1;
    }
    return 0;
}
コード例 #12
0
ファイル: voxeditor.cpp プロジェクト: Dakror/voxie
void VoxelEditor::use_tool_primary(bool click)
{
    int tool = window->get_tool();

    if ((tool == BLOCK_EDIT_TOOL || tool == BUCKET_EDIT_TOOL) && !click)
        return;

    if (tool == POINTER_EDIT_TOOL) {
        vec2 mouse(last_pos.x(), height() - last_pos.y());
        vec3 pos, dir;
        get_window_ray(mouse, inverse_mvp, viewport, pos, dir);

        if (selected_list.size() > 0) {
            if (click)
                pos_arrows.on_mouse_press(pos, dir);
            else {
                pos_arrows.on_mouse_move(pos, dir);
                vec3 add = pos_arrows.get(1.0f);
                int dx = int(add.x);
                int dy = int(add.y);
                int dz = int(add.z);
                offset_selected(dx, dy, dz);
            }
            if (pos_arrows.pan != NONE_CONE) {
                update();
                return;
            }
        }

        if (click) {
            start_drag = last_pos;
            rubberband->move(mapToGlobal(start_drag));
            rubberband->resize(0, 0);
            rubberband->show();
        } else {
            rubberband->setGeometry(
                QRect(mapToGlobal(start_drag), 
                mapToGlobal(last_pos)).normalized());
        }
        update_drag();
        return;
    }

    if (!has_hit)
        return;

    int hit_x, hit_y, hit_z;
    if (tool == BLOCK_EDIT_TOOL) {
        hit_x = hit_next.x;
        hit_y = hit_next.y;
        hit_z = hit_next.z;
    } else if (tool == PENCIL_EDIT_TOOL || tool == BUCKET_EDIT_TOOL) {
        if (hit_floor)
            return;
        hit_x = hit_block.x;
        hit_y = hit_block.y;
        hit_z = hit_block.z;
    } else
        return;

    if (tool == BUCKET_EDIT_TOOL)
        flood_fill(hit_x, hit_y, hit_z);
    else
        voxel->set(hit_x, hit_y, hit_z, window->get_palette_index());
    voxel->reset_shape();

    update_hit();
    on_changed();
}